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 घोषित करें - फ्रैगमेंट मैनिफेस्ट में घोषित नहीं होता।
ऐप चलाएं
संदेश टाइप करें, "डेटा भेजें" दबाएं, फ्रैगमेंट में संदेश दिखाई देगा।