Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a bundle to a Fragment from a FragmentActivity

Tags:

android

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 ?

like image 325
user1730789 Avatar asked Apr 17 '13 07:04

user1730789


2 Answers

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");
like image 97
Gene Avatar answered Nov 15 '22 05:11

Gene


Before ft.replace(R.id.fragment_content, fragment, fargmentTag); add the following line:

fragment.setArguments(bundle).

like image 28
hwrdprkns Avatar answered Nov 15 '22 06:11

hwrdprkns