How to transfer value from one fragment to another in Android?

I am new to Fragments. I want to pass a String value from one fragment to another. how to do it? I named my fragments as follows. please guide me step by step.

String cid = id.getText().toString(); Fragment fr = new FriendFragment(); android.app.FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.content_frame, fr); ft.commit(); 
+8
android android-fragments bundle
source share
5 answers

You can do something like below

  String cid=id.getText().toString(); Fragment fr=new friendfragment(); FragmentManager fm=getFragmentManager(); android.app.FragmentTransaction ft=fm.beginTransaction(); Bundle args = new Bundle(); args.putString("CID", cid); fr.setArguments(args); ft.replace(R.id.content_frame, fr); ft.commit(); 

To retrieve data, follow these steps:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String strtext = getArguments().getString("CID"); return inflater.inflate(R.layout.fragment, container, false); } 
+28
source share

If you want to send data from a fragment to an asset, you can use the interface.

But when you want to send data from a fragment to another fragment, it becomes complicated. You want to send the data to an action, and then to another fragment.

I use EventBus to solve this problem. How it works.

  • Create event-E.
  • From the Fragment-A register for events.
  • From snippet-B, post event-E with the data you want to transfer.
  • You will get the data in the onEvent () method that you wrote in Fragment-A.

What is it. No need to write your own interfaces.

You can also use eventbus to communicate from a background service or streams.

Checkout EventBus HowTo and the Repository .

+6
source share

Sending a fragment to a fragment must be done through parent activity.

FragmentToSendData strong>

 interface <interfaceName>{ void <abstract method>(String str); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try{ <instance of the interface> = (<interface>)getActivity(); }catch (ClassCastException e) { throw new ClassCastException(getActivity().toString() + " must implement <interface>"); } } 

ActivityWithBothFragments (it can be through ViewPager or use the identifier of your fragment, just use findFragmentById ())

 @Override <abstract method from frag1>(String str){ FragmentToReceiveData fragment2 = (FragmentToReceiveData)getSupportFragmentManager().findFragmentByTag(ViewPagerAdapter.getFragmentTag(1)); fragment2.getStringFromFrag1(String str); } 

FragmentToReceiveData STRONG>

 public void getStringFromFrag1(String str){ <textview>.setText("str"); } 
0
source share
 try this : - send data : Bundle arg = new Bundle(); arg.putInt("ID", "12"); //arg.putInt("KEY", "VALUE") arg.putString("NAME","Jaydeep");//arg.putString("KEY","VALUE") YourFragment fragment = new YourFragment(); fragment.setArguments(arg); fragmentManager.beginTransaction().add("PUT YOUR FRAM ID", fragment, fragment.getClass().getName()) .addToBackStack(null) .commit(); - receive data : Bundle bundle = getIntent().getExtras(); bundle.getInt("ID"); bundle.getString("NAME"); Log.e("Receive data : " ,"\nID - "+bundle.getInt("ID")+"\nNAME - "+bundle.getString("NAME")); 
0
source share

you can try it

First fragment

 //adding data to the bundle class Bundle b = new Bundle(); b.putString("user_name","simon"); b.putString("user_address","nepal"); //fragment operation FragmentTransaction transaction = getFragmentManager().beginTransaction(); Fragment fragment=new friendfragment(); fragment.setArguments(b); transaction.replace(R.id.container, fragment); transaction.addToBackStack(null); transaction.commit(); //Second Fragment(Receiving Fragment) //write following code into onCreateView() method Bundle b = getArguments(); String name = b.getString("user_name"); String address = b.getString("user_address"); 
0
source share

All Articles