Reputation: 5177
I have the following lines in my code
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, fragment, fargmentTag);
Now I want to add a bundle to my fragment. How can I do this ?
Upvotes: 7
Views: 11291
Reputation: 11285
Try this:
Anywhere inside your FragmentActivity class, put this:
MyFragmentClass mFrag = new MyFragmentClass();
Bundle bundle = new Bundle();
bundle.putString("DocNum", docNum); //parameters are (key, value).
mFrag.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.page_fragments, mFrag).commit();
I’m using “import android.support.v4.app.FragmentActivity;” so I use “getSupportFragmentManager()”. So to summarize the code above, u created a bundle instance and an instance of your fragment. Then you associated the two objects with “mFrag.setArguments(bundle)”. So now the “bundle” is associated with that instance of your MyFragmentClass. So anywhere in your MyFragmentClass, retrieve the bundle by calling:
Bundle bundle = getArguments();
String mDocNum = bundle.getString("DocNum");
Upvotes: 10
Reputation: 3236
Before calling replace
just add fragment.setArguments(bundle)
Upvotes: 0
Reputation: 7585
Before ft.replace(R.id.fragment_content, fragment, fargmentTag);
add the following line:
fragment.setArguments(bundle)
.
Upvotes: 5