Android Fragment 中的 getIntent().getExtras() 在哪里/如何? [英] Where/How to getIntent().getExtras() in an Android Fragment?

查看:28
本文介绍了Android Fragment 中的 getIntent().getExtras() 在哪里/如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于活动,我曾经这样做过:

With Activities, I used to do this:

在活动 1 中:

Intent i = new Intent(getApplicationContext(), MyFragmentActivity.class);
                i.putExtra("name", items.get(arg2));
                i.putExtra("category", Category);
                startActivity(i);

在活动 2 中:

Item = getIntent().getExtras().getString("name");

你如何使用 Fragment 做到这一点?我也在使用兼容库 v4.

How do you do this using Fragments? I am using the compatibility library v4 also.

它是否在 FragmentActivity 中?还是真正的Fragment?它进入哪个方法?在创建?在创建视图?另一个?

Does it go in the FragmentActivity? Or the actual Fragment? And Which Method does it go in? onCreate? onCreateView? another?

我可以看看示例代码吗?

And can I see example code please?

值得注意的是,我试图将 Activity 1 保留为一个 Activity(或者实际上是 ListActivity,其中我在单击时传递列表项的意图),然后传递给一组选项卡式片段(通过 Fragment Activity) 并且我需要任一选项卡才能获得附加功能.(我希望这是可能的吗?)

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

推荐答案

我倾向于做的事情,而且我相信这也是 Google 希望开发人员做的事情,就是仍然从 IntentActivity 中,然后通过使用参数实例化它们将任何额外的数据传递给片段.

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.

实际上有一个例子在说明此概念的 Android 开发博客上,您也将在多个 API 演示中看到这一点.尽管此特定示例是针对 API 3.0+ 片段给出的,但在使用支持库中的 FragmentActivityFragment 时,同样的流程适用.

There's actually an example on 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.

您首先像往常一样在您的活动中检索意图附加内容,并将它们作为参数传递给片段:

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

与直接调用构造函数相比,使用静态方法为您将参数插入片段中可能更容易.这种方法在 newInstance="noreferrer">Google 提供的示例.DetailsFragment 中实际上有一个 newInstance 方法,所以我不确定为什么上面的代码片段中没有使用它...

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...

无论如何,在创建片段时作为参数提供的所有额外内容都可以通过调用 getArguments() 获得.由于这返回一个 Bundle,它的用法类似于 Activity 中的 extras.

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.

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)

}

这篇关于Android Fragment 中的 getIntent().getExtras() 在哪里/如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆