9

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?

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
y07k2
  • 1,898
  • 4
  • 20
  • 36

4 Answers4

31

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);
Mohsen fallahi
  • 915
  • 1
  • 10
  • 27
4

FragmentTransaction is simply to transition Fragments, it doesn't "pass" anything. You need to use a Bundle.

SettingsBlockedUsersFragment frag = new SettingsBlockedUsersFragment();
Bundle b = new Bundle();
// put stuff into bundle...
b.putString("user", "steve");

// Pass the bundle to the Fragment
frag.setArguments(b);

// Use Fragment Transaction
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, frag, FRAGMENT_TAG);
ft.commit();

Then inside the onCreate of the Fragment, you can do

String user = getArguments().getString("user");

Other ways to pass data into a Fragment are discussed at Best practice for instantiating a new Android Fragment

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

you can use a factory method in your fragment class(as recommended in the docs)

public static YourFragment newInstance(String sample) {
    YourFragment fragment = new YourFragment();
    Bundle args = new Bundle();
    args.putString(KEY, sample);
    fragment.setArguments(args);
    return fragment;
}

then call YourFragment.newInstance("string you need to pass"); to get an instance of your fragment and use that in your transaction.

The key here is actually using the setArguments() method to pass in data. you can then retrieve this data in your fragment's oncreate method like this

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        streamSource = getArguments().getParcelable(KEY);
    }
}
harshitpthk
  • 4,058
  • 2
  • 24
  • 32
0

In your SettingsBlockedUsersFragment class have a static method like the following

public class SettingsBlockedUsersFragment extends Fragment {
    private static final String MY_STRING = "my_string";


    public static SettingsBlockedUsersFragment newInstance(String yourStringValue){
        SettingsBlockedUsersFragment frag = new SettingsBlockedUsersFragment();
        Bundle args = new Bundle();
        args.putString(MY_STRING, yourStringValue);
        frag.setArguments(args);
        return frag;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
          Bundle args = getArguments();
          if(args != null){
               String myString = args.getString(MY_STRING);
          }
    }
}

In your fragment transaction you can pass the args like this

final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content,SettingsBlockedUsersFragment.newInstance("my string"), FRAGMENT_TAG);
ft.commit();
Much Overflow
  • 3,142
  • 1
  • 23
  • 40