安卓:将参数传递给一个标签参数、标签

2023-09-06 09:04:44 作者:那余伤未散

在我的Andr​​oid应用程序,我用下面的code创建标签:

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

addTab 的方法,第三个参数是一个Bundle对象,是。我可以用这个第三个参数的参数传递给我的片段?

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 ?

Android的API文档为空 addTab 并不会记录此参数。

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

推荐答案

答案是肯定的。要传递这个参数捆绑稍后设为您的片段参数,可与getArguments从该片段内。

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.

在code,使得它发生在FragmentTabHost是:

The code that makes it happen in the FragmentTabHost is :

newTab.fragment = Fragment.instantiate(mContext,
                        newTab.clss.getName(), newTab.args);