Android Fragment 中的 getIntent().getExtras() 在哪里/如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11387740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where/How to getIntent().getExtras() in an Android Fragment?
提问by TheLettuceMaster
With Activities, I used to do this:
通过活动,我曾经这样做过:
In Activity 1:
在活动 1 中:
Intent i = new Intent(getApplicationContext(), MyFragmentActivity.class);
i.putExtra("name", items.get(arg2));
i.putExtra("category", Category);
startActivity(i);
In Activity 2:
在活动 2 中:
Item = getIntent().getExtras().getString("name");
How do you do this using Fragments? I am using the compatibility library v4 also.
你如何使用 Fragments 做到这一点?我也在使用兼容性库 v4。
Does it go in the FragmentActivity? Or the actual Fragment? And Which Method does it go in? onCreate? onCreateView? another?
它是否在 FragmentActivity 中?还是真正的Fragment?它进入哪个方法?在创建?在创建视图?其他?
And can I see example code please?
我可以看看示例代码吗?
EDIT: It is worth noting I am trying to keep Activity 1 as an Activity (or actually ListActivity where I am passing the intent of the listitem when clicked) and then pass to a set of tabbed-fragments (through a Fragment Activity) and I need either tab to be able to get the extras. (I hope this is possible?)
编辑:值得注意的是,我试图将 Activity 1 保留为一个 Activity(或者实际上是 ListActivity,其中我在单击时传递列表项的意图),然后传递给一组选项卡式片段(通过 Fragment Activity)和我需要任一选项卡才能获得附加功能。(我希望这是可能的吗?)
回答by meeeee
you can still use
你仍然可以使用
String Item = getIntent().getExtras().getString("name");
in the fragment
, you just need call getActivity()
first:
在 中fragment
,您只需getActivity()
要先致电:
String Item = getActivity().getIntent().getExtras().getString("name");
This saves you having to write some code.
这使您不必编写一些代码。
回答by MH.
What I tend to do, and I believe this is what Google intended for developers to do too, is to still get the extras from an Intent
in an Activity
and then pass any extra data to fragments by instantiating them with arguments.
我倾向于做的事情,而且我相信这也是 Google 打算让开发人员做的事情,仍然是从Intent
in an 中获取Activity
额外数据,然后通过使用参数实例化它们将任何额外数据传递给片段。
There's actually an exampleon the Android dev blog that illustrates this concept, and you'll see this in several of the API demos too. Although this specific example is given for API 3.0+ fragments, the same flow applies when using FragmentActivity
and Fragment
from the support library.
实际上,Android 开发博客上有一个示例说明了这个概念,您也会在多个 API 演示中看到这一点。尽管此特定示例是针对 API 3.0+ 片段给出的,但在使用FragmentActivity
和Fragment
来自支持库时也适用相同的流程。
You first retrieve the intent extras as usual in your activity and pass them on as arguments to the fragment:
您首先像往常一样在您的活动中检索意图附加内容,并将它们作为参数传递给片段:
public static class DetailsActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// (omitted some other stuff)
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(
android.R.id.content, details).commit();
}
}
}
In stead of directly invoking the constructor, it's probably easier to use a static method that plugs the arguments into the fragment for you. Such a method is often called newInstance
in the examples given by Google. There actually is a newInstance
method in DetailsFragment
, so I'm unsure why it isn't used in the snippet above...
与直接调用构造函数不同,使用静态方法为您将参数插入片段中可能更容易。这种方法newInstance
在谷歌给出的例子中经常被调用。中实际上有一个newInstance
方法DetailsFragment
,所以我不确定为什么在上面的代码片段中没有使用它......
Anyways, all extras provided as argument upon creating the fragment, will be available by calling getArguments()
. Since this returns a Bundle
, its usage is similar to that of the extras in an Activity
.
无论如何,在创建片段时作为参数提供的所有附加功能都可以通过调用getArguments()
. 由于这返回 a Bundle
,因此其用法类似于a中的附加项的用法Activity
。
public static class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
// (other stuff omitted)
}