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 ?
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");
Before ft.replace(R.id.fragment_content, fragment, fargmentTag);
add the following line:
fragment.setArguments(bundle)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With