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.
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);
Looking at the FragmentTabHost.java,looks like it passes this bundle to the tabinfo,which inturn has the fragment.So the answer is yes !
来源:https://stackoverflow.com/questions/16562234/android-passing-parameters-to-a-tab