Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में एक Fragment से दूसरे Fragment में डेटा कैसे भेजें?

परिचय

यह ट्यूटोरियल दर्शाता है कि Android में Fragment के बीच डेटा कैसे पास करें। हम ViewPager और TabLayout का उपयोग करके दो फ्रैगमेंट्स बनाएंगे और इंटरफेस के माध्यम से डेटा ट्रांसफर करेंगे।

आवश्यक चरण

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनें।

चरण 2: मुख्य लेआउट (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed" />

    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

चरण 3: दो फ्रैगमेंट लेआउट और क्लासेस बनाएं

फ्रैगमेंट वन - डेटा भेजने वाला (FragmentOne.java)

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class FragmentOne extends Fragment {
    private SendMessage sendMessage;

    public FragmentOne() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_fragment_one, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button btnPassData = view.findViewById(R.id.btnPassData);
        final EditText inData = view.findViewById(R.id.passMessage);

        btnPassData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage.sendData(inData.getText().toString().trim());
            }
        });
    }

    // इंटरफेस: एक्टिविटी को इम्प्लीमेंट करना होगा
    interface SendMessage {
        void sendData(String message);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        try {
            sendMessage = (SendMessage) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException("डेटा प्राप्त करने में त्रुटि। कृपया पुनः प्रयास करें");
        }
    }
}

fragment_fragment_one.xml

<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/passMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/btnPassData"
            android:layout_margin="16dp"
            android:hint="यहां टाइप करें" />

        <Button
            android:id="@+id/btnPassData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="फ्रैगमेंट टू को डेटा भेजें" />

    </RelativeLayout>
</ScrollView>

फ्रैगमेंट टू - डेटा प्राप्त करने वाला (FragmentTwo.java)

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentTwo extends Fragment {
    private TextView textView;

    public FragmentTwo() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_fragment_two, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textView = view.findViewById(R.id.txtData);
    }

    // एक्टिविटी से डेटा प्राप्त करने के लिए पब्लिक मेथड
    void displayReceivedData(String message) {
        textView.setText("प्राप्त डेटा: " + message);
    }
}

fragment_fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentTwo">

    <TextView
        android:id="@+id/txtData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:text="कोई डेटा प्राप्त नहीं हुआ" />

</RelativeLayout>

चरण 4: मुख्य गतिविधि (src/MainActivity.java)

package app.com.sample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity implements FragmentOne.SendMessage {
    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);

        tabLayout = findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);
    }

    // FragmentOne.SendMessage इंटरफेस का इम्प्लीमेंटेशन
    @Override
    public void sendData(String message) {
        // ViewPager में फ्रैगमेंट टैग द्वारा ढूंढें
        String tag = "android:switcher:" + R.id.viewPager + ":" + 1; // इंडेक्स 1 = दूसरा फ्रैगमेंट
        FragmentTwo f = (FragmentTwo) getSupportFragmentManager().findFragmentByTag(tag);
        if (f != null) {
            f.displayReceivedData(message);
        }
    }
}

नोट: आपको एक ViewPagerAdapter क्लास भी बनानी होगी जो `FragmentPagerAdapter` या `FragmentStatePagerAdapter` एक्सटेंड करे और दोनों फ्रैगमेंट्स को मैनेज करे।

चरण 5: मैनिफेस्ट फ़ाइल (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

ऐप चलाएं

डिवाइस कनेक्ट करें, Run क्लिक करें। पहले टैब में टेक्स्ट टाइप करें, बटन दबाएं - दूसरा टैब उस टेक्स्ट को दिखाएगा।

Android में एक Fragment से दूसरे Fragment में डेटा कैसे भेजें?

मुख्य अवधारणाएं

  • इंटरफेस कॉलबैक - फ्रैगमेंट एक्टिविटी से कम्युनिकेट करता है।
  • ViewPager एडाप्टर - फ्रैगमेंट्स को स्वाइप करके नेविगेट करने के लिए।
  • findFragmentByTag - टैग द्वारा फ्रैगमेंट इंस्टेंस प्राप्त करने के लिए।
  • आधुनिक विकल्प: ViewModel (शेयर्ड) या Fragment Result API (setFragmentResult) का उपयोग करें।
  1. Android में एक एक्टिविटी से दूसरी एक्टिविटी में ऑब्जेक्ट कैसे पास करें? Android में एक एक्टिविटी से दूसरी एक्टिविटी में ऑब्जेक्ट कैसे पास करें?

    यह उदाहरण दर्शाता है कि Android में Serializable इंटरफेस का उपयोग करके कस्टम ऑब्जेक्ट को एक एक्टिविटी से दूसरी एक्टिविटी में कैसे पास किया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML (MainActivity) res/layout/activity_main.xml: <?xml version=1.0 enco

  1. एंड्रॉइड में इंटेंट का उपयोग करके एक एक्टिविटी से दूसरी एक्टिविटी में ऑब्जेक्ट कैसे भेजें? एंड्रॉइड में इंटेंट का उपयोग करके एक एक्टिविटी से दूसरी एक्टिविटी में ऑब्जेक्ट कैसे भेजें?

    Serializable के साथ एक्टिविटीज के बीच ऑब्जेक्ट पास करना यह उदाहरण दर्शाता है कि Serializable इंटरफेस को लागू करके कस्टम ऑब्जेक्ट को इंटेंट के माध्यम से एक एक्टिविटी से दूसरी एक्टिविटी में कैसे भेजें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मेन लेआउट में EditT

  1. एंड्रॉइड में एक एक्टिविटी से दूसरी एक्टिविटी में इमेज कैसे पास करें एंड्रॉइड में एक एक्टिविटी से दूसरी एक्टिविटी में इमेज कैसे पास करें

    Intent के माध्यम से इमेज रिसोर्स ID या बिटमैप पास करना एंड्रॉइड में एक एक्टिविटी से दूसरी में इमेज पास करने के दो मुख्य तरीके हैं: रिसोर्स ID (इंट/ड्रॉएबल रेफरेंस) पास करना (कुशल, केवल ऐप-इंटरनल इमेज के लिए) या बिटमैप/बाइट ऐरे पास करना (किसी भी इमेज के लिए, लेकिन मेमोरी-इंटेंसिव)। चरण 1: नया प्रोजे