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

एंड्रॉइड में एक्टिविटी से फ्रैगमेंट में वेरिएबल कैसे पास करें?

Bundle और setArguments के साथ फ्रैगमेंट को डेटा पास करें

यह उदाहरण दर्शाता है कि Bundle का उपयोग करके एक्टिविटी से फ्रैगमेंट में स्ट्रिंग डेटा कैसे पास करें।

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

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

चरण 2: लेआउट में कंटेनर और इनपुट

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="2dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_marginTop="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="अपना संदेश दर्ज करें" />

    <Button
        android:id="@+id/btnSendData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="डेटा भेजें" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</LinearLayout>

चरण 3: MainActivity.java - फ्रैगमेंट में डेटा पास करें

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    EditText editText;
    Button button;

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

        final FragmentManager fragmentManager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        final MyFragment myFragment = new MyFragment();

        button = findViewById(R.id.btnSendData);
        editText = findViewById(R.id.editText);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();
                b.putString("message", editText.getText().toString());
                myFragment.setArguments(b);
                fragmentTransaction.add(R.id.frameLayout, myFragment).commit();
            }
        });
    }
}

चरण 4: फ्रैगमेंट क्लास (MyFragment.java)

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {
    TextView textView;

    public MyFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        textView = view.findViewById(R.id.textView);

        Bundle bundle = getArguments();
        String message = bundle.getString("message");
        textView.setText(message);
        return view;
    }
}

चरण 5: फ्रैगमेंट लेआउट (fragment_my.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/textView"
        android:textSize="30dp"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

चरण 6: मैनिफेस्ट

केवल MainActivity घोषित करें - फ्रैगमेंट मैनिफेस्ट में घोषित नहीं होता।

ऐप चलाएं

संदेश टाइप करें, "डेटा भेजें" दबाएं, फ्रैगमेंट में संदेश दिखाई देगा।

एंड्रॉइड में एक्टिविटी से फ्रैगमेंट में वेरिएबल कैसे पास करें?

  1. Android में ड्रॉएबल (Drawable) को एक्टिविटी के बीच कैसे पास करें? Android में ड्रॉएबल (Drawable) को एक्टिविटी के बीच कैसे पास करें?

    Android में Intent के माध्यम से ड्रॉएबल संसाधन पास करने का गाइड यह ट्यूटोरियल दिखाता है कि कैसे ड्रॉएबल रिसोर्स ID (इंटीजर) को Intent extra के रूप में दूसरी एक्टिविटी में पास किया जाए और वहां ImageView में सेट किया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रो

  1. एंड्रॉइड में फ्रैगमेंट से एक्टिविटी का मेथड कैसे कॉल करें एंड्रॉइड में फ्रैगमेंट से एक्टिविटी का मेथड कैसे कॉल करें

    फ्रैगमेंट से होस्ट एक्टिविटी के पब्लिक मेथड को कॉल करने के तरीके एंड्रॉइड में फ्रैगमेंट अपनी होस्ट एक्टिविटी के साथ getActivity() के माध्यम से संवाद कर सकता है। एक्टिविटी के पब्लिक मेथड को कॉल करने के लिए फ्रैगमेंट को एक्टिविटी को उपयुक्त टाइप में कास्ट करना होता है। यह पैटर्न फ्रैगमेंट-एक्टिविटी सं

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

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