Android : passing parameters to a tab

…衆ロ難τιáo~ 提交于 2019-12-22 05:43:13

问题


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.


回答1:


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);



回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!