Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : passing parameters to a tab

In my Android app, I use the following code to create tabs :

mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
            MyFragment.class, null);

In the addTab method, the third parameter is a Bundle object and is null. Could I use this third parameter to pass parameters to my fragment ?

The android API documentation is empty for addTab and does not document this parameter.

like image 282
Arnaud Avatar asked May 15 '13 10:05

Arnaud


2 Answers

The answer is yes. the parameters you are passing in this Bundle are later set as your fragment arguments and can be accessed with getArguments from inside the fragment.

The code that makes it happen in the FragmentTabHost is :

newTab.fragment = Fragment.instantiate(mContext,
                        newTab.clss.getName(), newTab.args);
like image 141
Sean Avatar answered Nov 20 '22 13:11

Sean


Looking at the FragmentTabHost.java,looks like it passes this bundle to the tabinfo,which inturn has the fragment.So the answer is yes !

like image 1
Kunal Khaire Avatar answered Nov 20 '22 13:11

Kunal Khaire