質問

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 !

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top