传递一个自定义的对象从一个活动到另一个Parcelable VS捆绑 [英] Passing a custom Object from one Activity to another Parcelable vs Bundle

查看:137
本文介绍了传递一个自定义的对象从一个活动到另一个Parcelable VS捆绑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个活动传递一个自定义对象到另一个对象由一个字符串和其他自定义对象,它包含一个字符串数组和int数组的列表中。我读过 http://stackoverflow.com/a/2141166/830104 的,但后来我发现这个答案 http://stackoverflow.com/a/7842273/830104 。这是更好地利用捆绑或Parcelable?区别是什么?什么时候应该使用这个类?
感谢您的答复,

I'd like to pass a custom Object from one activity to another, the Object consists of a String and a List of another custom Object which consists of an array of strings and an array of ints. I've read http://stackoverflow.com/a/2141166/830104, but then I've found this answer http://stackoverflow.com/a/7842273/830104. Which is better to use Bundle or Parcelable? What is the difference? When should I use this each?
Thanks for your replies,
Dan

推荐答案

Parcelable 捆绑不是唯一的概念;你甚至可以在同一时间同时部署您的应用程序。

Parcelable and Bundle are not exclusive concepts; you can even deploy both on your app at a time.

[1]期限 Parcelable 自带的Java序列的概念(和其它高级语言如C#,Python的...)。它确保了一个对象 - 它仍然在RAM存储 - 例如 Parcelable 类都可以的保存文件流,如文本或内存(脱机状态)然后可以重构在运行时(在线状态),以用于程序

[1] Term Parcelable comes with Serialization concept in Java (and other high-level language such as C#, Python,...). It ensures that an object - which remains in RAM store - of such Parcelable class can be saved in file stream such as text or memory (offline status) then can be reconstructed to be used in program at runtime (online status).

在Android应用程序,在2 独立活动(仅运行 - 一个人开始,然后对方将不得不停止):

In an Android application, within 2 independent activities (exclusively running - one starts then other will have to stop):

届时将有来自当前的活动是指以previous之一,其成员没有指针 - 因为previous活动都已停止,并清理出的形式存储;这样为维护对象的值传递到下一个活动(从名为意图)的对象必须是 parcelable 序列化)。

There will be NO pointer from current activity to refer to previous one and its members - because previous activity is stopped and cleared out form memory; so that to maintain object's value passed to next activity (called from Intent) the object need to be parcelable (serializable).

[2]虽然 捆绑 通常是Android的概念,是指一个变量或一组变量。如果考虑较低的水平,它可以被看作是HashMap的键 - 值对。

[2] While Bundle is normally the Android concept, denotes that a variable or group of variables. If look into lower level, it can be considered as HashMap with key-value pairs.

结论:

  • 捆绑 是存储多个对象与相关按键,它可以保存原生类型的任何对象,但它的不知道如何保存复杂的对象(其中包含一个ArrayList为例)

  • Bundle is to store many objects with related keys, it can save any object in native types, but it doesn't know how to save a complex object (which contains an ArrayList for example)

Parcelable 类是确保一个复杂的实例也可以是 连载 反序列化 在运行时。这可以对象包含复杂类型,例如ArrayList,HashMap中,数组或结构,...

Parcelable class is to ensure a complex instance of it can be serialized and de-serialized during runtime. This object can contains complex types such as ArrayList, HashMap, array, or struct,...

[增订] - 例:

//Class without implementing Parcelable will cause error 
//if passing though activities via Intent
public class NoneParcelable
{
    private ArrayList<String> nameList = new ArrayList<String>();
    public NoneParcelable()
    {
        nameList.add("abc");
        nameList.add("xyz");            
    }
}

//Parcelable Class's objects can be exchanged 
public class GoodParcelable implements Parcelable
{
    private ArrayList<String> nameList = new ArrayList<String>();
    public GoodParcelable()
    {
        nameList.add("Can");
        nameList.add("be parsed");          
    }
    @Override
    public int describeContents()
    {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
        // Serialize ArrayList name here            
    }
}

在源活动:

NoneParcelable nonePcl = new NoneParcelable();
GoodParcelable goodPcl = new GoodParcelable();
int count = 100;

Intent i = new Intent(...);
i.putExtra("NONE_P",nonePcl);
i.putExtra("GOOD_P",nonePcl);
i.putExtra("COUNT", count);

在目的地的活动:

Intent i = getIntent();

//this is BAD:
NoneParcelable nP = (NoneParcelable)i.getExtra("NONE_P"); //BAD code

//these are OK:
int count = (int)i.getExtra("COUNT");//OK
GoodParcelable myParcelableObject=(GoodParcelable)i.getParcelableExtra("GOOD_P");// OK

这篇关于传递一个自定义的对象从一个活动到另一个Parcelable VS捆绑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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