I have an Activity with two fragments and I need to pass a string from FragmentA to FragmentB.
To pass the data, I have this in my FragmentA:
Intent intent = new Intent(getActivity(), FragmentB.class);
intent.putExtra("name", "Mark");
startActivity(intent);
And to get the data, I did this in FragmentB
Intent intent = getActivity().getIntent();
Bundle b = intent.getExtras();
if(b!=null)
{
String name =(String) b.get("name");
nameTextView.setText(name);
}
But this is not working. Is there a specific way to pass a string from one fragment to another fragment?
You can do something like below,
To receive the data do the following,
Code in FragmentActivity 1 fragment A:
fb is the instance of the bean I created and ID is the parameter
Code in FragmentActivity 2 fragment Any:
to pass data between Fragments you can use
setArguments(Bundle b)
. For instance: