Peter Chappy
Peter Chappy

Reputation: 1179

How do I pass a bundle to another fragment using getFragmentManager

I have a nav fragment and a content fragment. I want to be able to pass a bundle, and reload the fragment based on fragment interactions. I've seen people say use .setArguments(), but it's not letting me use said method. Any advice?

public void onStockFragmentInteraction(String stock) {
    Bundle bundle = new Bundle();
    bundle.putString("stock", stock);

    getFragmentManager()
            .beginTransaction()
            .replace(R.id.content, new StoryFragment())
            .addToBackStack(null)
            .commit();
}

Upvotes: 1

Views: 209

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

setArguments is a method of Fragment not of FragmentManager

StoryFragment f = new StoryFragment() 
f.setArguments(bundle);

getFragmentManager()
        .beginTransaction()
        .replace(R.id.content, f)
        .addToBackStack(null)
        .commit();

keep a reference to the fragment and call setArguments on the instance before submitting it on the transaction

Upvotes: 2

Related Questions