Send data to fragment with FragmentTransaction

y07k2 picture y07k2 · Mar 16, 2016 · Viewed 14.2k times · Source

I'm in my fragment class calling this:

@OnClick(R.id.blockedLinkLayout)
public void onBlockedClick(){
    final FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.content, new SettingsBlockedUsersFragment(), FRAGMENT_TAG);
    ft.commit();
}

And it just replace my current fragment with chosen one.

And my question is, how can I send some data (e.g. String value) from my parent fragment to my child fragment using FragmentTransaction?

Answer

Mohsen fallahi picture Mohsen fallahi · Mar 16, 2016

Just pass them in a bundle as fragment arguments

in parent fragment :

SettingsBlockedUsersFragment fragment = new SettingsBlockedUsersFragment();
Bundle arguments = new Bundle();
arguments.putString( string_key , desired_string);
fragment.setArguments(arguments);
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, fragment , FRAGMENT_TAG);
ft.commit();

in child fragment :

Bundle arguments = getArguments();
String desired_string = arguments.getString(string_key);