Android 将对象传递给片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10836525/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:10:12  来源:igfitidea点击:

Passing objects in to Fragments

androidfragmentandroid-bundle

提问by Martyn

I've been working with lots of Fragmentsrecently and have been using two distinct methods of passing in objects to the Fragments, but the only difference that I can see is that in the approach taken by FragmentOne below, the object you pass in must implement the Serializableinterface (and everything associated with that).

Fragments最近一直在使用两种不同的方法将对象传递给 Fragment,但我能看到的唯一区别是,在下面的 FragmentOne 采用的方法中,您传递的对象必须实现Serializable界面(以及与之相关的所有内容)。

Are there any benefits to using one over the other?

使用其中一种有什么好处吗?

public class FragmentOne extends Fragment {
    public static final String FRAGMENT_BUNDLE_KEY = 
        "com.example.FragmentOne.FRAGMENT_BUNDLE_KEY";

    public static FragmentOne newInstance(SomeObject someObject) {
        FragmentOne f = new FragmentOne();
        Bundle args = new Bundle();
        args.putSerializable(FRAGMENT_BUNDLE_KEY, someObject);
        f.setArguments(args);
        return f;
    }

    public SomeObject getSomeObject() {
        return (SomeObject) getArguments().getSerializable(FRAGMENT_BUNDLE_KEY);
    }
}

and

public class FragmentTwo extends Fragment {
    SomeObject mSomeObject;  

    public static FragmentTwo newInstance(SomeObject someObject) {
        FragmentTwo fragment = new FragmentTwo();
        fragment.setSomeObject(someObject);
        return fragment;
    }

    public void setSomeObject(SomeObject someObject) {
        mSomeObject = someObject;
    }
}

回答by logcat

There are 3 ways to pass objects to a fragment

有 3 种方法可以将对象传递给片段

They are:

他们是:

  1. Passing the object through a setter is the fastest way, but state will not be restored automatically.
  2. setArgumentswith Serializableobjects is the slowest way (but okay for small objects, I think) and you have automatic state restoration.
  3. Passing as Parcelableis a fast way (prefer it over 2nd one if you have collection of elements to pass), and you have automatic state restoration.
  1. 通过 setter 传递对象是最快的方式,但状态不会自动恢复。
  2. setArguments使用Serializable对象是最慢的方式(但我认为对于小对象还可以),并且您可以自动恢复状态。
  3. 按原样传递Parcelable是一种快速的方式(如果您有要传递的元素集合,则更喜欢第 2 种方式),并且您可以自动恢复状态。

http://developer.android.com/reference/android/os/Parcelable.html

http://developer.android.com/reference/android/os/Parcelable.html

回答by Adnan Abdollah Zaki

for Collection such as List :

对于诸如 List 之类的集合:

I wanted to share my experience.

我想分享我的经验。

you need to implement Parcelable

你需要实现 Parcelable

Just use the putParcelableArrayList method.

只需使用 putParcelableArrayList 方法。

ArrayList<LClass> localities = new ArrayList<LClass>;
...
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(KEY_LClass_LIST, localities);
fragmentInstance.setArguments(bundle);

return fragmentInstance;

And retrieve it using...

并使用...检索它

localities = savedInstanceState.getParcelableArrayList(KEY_LCLass_LIST);

So, unless you need the custom ArrayList for some other reason, you can avoid doing any of that extra work and only implement Parcelable for your Locality class.

因此,除非出于某种其他原因需要自定义 ArrayList,否则您可以避免做任何额外的工作,只为您的 Locality 类实现 Parcelable。