在以片段传递对象 [英] Passing objects in to Fragments

查看:127
本文介绍了在以片段传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在与许多片段最近并一直在使用传递对象到片段的两种不同的方法,但是我能看到的唯一的区别是在采取以下FragmentOne的方法,你传递必须实现序列化接口(相关TopicAction.do?Id=45">and一切)。

在那里用一个比其他任何好处?

 公共类FragmentOne扩展片段{
    公共静态最后弦乐FRAGMENT_BUNDLE_KEY =
        com.example.FragmentOne.FRAGMENT_BUNDLE_KEY;

    公共静态FragmentOne的newInstance(SomeObject someObject){
        FragmentOne F =新FragmentOne();
        捆绑的args =新包();
        args.putSerializable(FRAGMENT_BUNDLE_KEY,someObject);
        f.setArguments(参数);
        返回F;
    }

    公共SomeObject getSomeObject(){
        返程(SomeObject)getArguments()getSerializable(FRAGMENT_BUNDLE_KEY)。
    }
}
 

 公共类FragmentTwo扩展片段{
    SomeObject mSomeObject;

    公共静态FragmentTwo的newInstance(SomeObject someObject){
        FragmentTwo片段=新FragmentTwo();
        fragment.setSomeObject(someObject);
        返回片段;
    }

    公共无效setSomeObject(SomeObject someObject){
        mSomeObject = someObject;
    }
}
 

解决方案

有3种方式对象传递给片段

它们是:

  1. 通过制定者传递的对象是最快的方式,但状态不会自动恢复。
  2. setArguments 序列化对象是最慢的方式(但没关系小物件,我认为),你有自动状态的恢复。
  3. 传递为 Parcelable 是一个快速的方法(preFER过来:​​第二个,如果你有元素集合来传递),你必须自动状态恢复。

<一个href="http://developer.android.com/reference/android/os/Parcelable.html">http://developer.android.com/reference/android/os/Parcelable.html

I've been working with lots of Fragments recently 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 Serializable interface (and everything associated with that).

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

解决方案

There are 3 ways to pass objects to a fragment

They are:

  1. Passing the object through a setter is the fastest way, but state will not be restored automatically.
  2. setArguments with Serializable objects is the slowest way (but okay for small objects, I think) and you have automatic state restoration.
  3. Passing as Parcelable is a fast way (prefer it over 2nd one if you have collection of elements to pass), and you have automatic state restoration.

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

这篇关于在以片段传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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