在哪里/如何getIntent()。getExtras()在Android碎片?碎片、getIntent、getExtras、Android

2023-09-12 04:20:50 作者:Barefaced

通过活动,我以前做的:

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

你如何使用片段做到这一点?我使用兼容性库V4也。

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

这是否走在FragmentActivity?或者实际的片段? 和这方法它进去吗?的onCreate? onCreateView?另一个?

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

和我能看到例如code吗?

And can I see example code please?

编辑:值得注意的是,我试图保持活动1作为一种活动(或实际ListActivity我在哪里传递的ListItem的意图单击时),然后传递到一组选项卡,碎片(通过一个片段活动),我需要或者标签,能够得到的花絮。 (我希望这是可能的吗?)

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

推荐答案

我倾向于做的,我相信这是谷歌面向开发人员做太多,就是仍然得到额外从活动意图 ,然后通过带参数的实例化它们传递任何额外的数据片段。

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+片段,同样的流程时,使用适用 FragmentActivity 片段从支持库。

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 中的由谷歌给出的例子。这里其实是一个的newInstance 方法 DetailsFragment ,所以我不能确定它为什么没有在片段中使用上述......

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()。由于此返回捆绑,它的用法类似于在活动的群众演员

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)

}