How to send data from one Fragment to another Fragment?

You have to attach your bundle to your fragment.

fragment.setArguments(bundle);

and after that change or replace the new fragment.

You have to be sure that the String is in the new Fragment. Debugging!!


1.If fragments are hosted by same activity- You cannot cast an intent to Fragment. Fragment acts as a part of Activity, it is not an activity by itself. So to share a string between fragments you can declare a static String in Activity. Access that string from Fragment A to set the value and Get the string value in fragment B.

2.Both fragments are hosted by different Activities- Then you can use putExtra to pass a string from Fragment A of Activity A to Activity B. Store that string in Activity B and use it in Fragment B.


Use Bundle to send String:

//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);

//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();

In onCreateView of the new Fragment:

//Retrieve the value
String value = getArguments().getString("YourKey");