本文翻译自:Android: Difference between Parcelable and Serializable?

Why does Android provide 2 interfaces for serializing objects? Android为什么提供2个用于序列化对象的接口? Do Serializable objects interopt with Android Binder and AIDL files? 可序列化对象与Android Binder和AIDL文件互斥吗?


#1楼

参考:https://stackoom.com/question/dWtY/Android-可拆分和可序列化之间的区别


#2楼

you can use the serializable objects in the intents but at the time of making serialize a Parcelable object it can give a serious exception like NotSerializableException. 您可以在意图中使用可序列化的对象,但是在序列化一个Parcelable对象时,它会产生严重的异常,例如NotSerializableException。 Is it not recommended using serializable with Parcelable . 不建议与Parcelable一起使用serializable。 So it is better to extends Parcelable with the object that you want to use with bundle and intents. 因此,最好使用要与bundle和intent一起使用的对象扩展Parcelable。 As this Parcelable is android specific so it doesn't have any side effects. 由于此Parcelable是android特有的,因此没有任何副作用。 :) :)


#3楼

If you want to be a good citizen, take the extra time to implement Parcelable since it will perform 10 times faster and use less resources. 如果您想成为一个好公民,请花额外的时间来实施Parcelable,因为它的执行速度提高了10倍,并且使用的资源更少。

However, in most cases, the slowness of Serializable won't be noticeable. 但是,在大多数情况下,Serializable的速度不会很明显。 Feel free to use it but remember that serialization is an expensive operation so keep it to a minimum. 可以随意使用它,但是请记住,序列化是一项昂贵的操作,因此请尽量减少序列化。

If you are trying to pass a list with thousands of serialized objects, it is possible that the whole process will take more than a second. 如果您试图传递包含数千个序列化对象的列表,则整个过程可能会花费一秒钟以上的时间。 It can make transitions or rotation from portrait to lanscape feel very sluggish. 它可以使从肖像到风景的过渡或旋转非常缓慢。

Source to this point: http://www.developerphil.com/parcelable-vs-serializable/ 到这一点的来源: http : //www.developerphil.com/parcelable-vs-serializable/


#4楼

In Android we cannot just pass objects to activities. 在Android中,我们不能仅将对象传递给活动。 To do this the objects must either implement Serializable or Parcelable interface. 为此,对象必须实现SerializableParcelable接口。

Serializable 可序列化

Serializable is a standard Java interface. Serializable是标准的Java接口。 You can just implement Serializable interface and add override methods. 您可以只实现Serializable接口并添加重写方法。 The problem with this approach is that reflection is used and it is a slow process. 这种方法的问题在于使用了反射,这是一个缓慢的过程。 This method creates a lot of temporary objects and causes quite a bit of garbage collection. 此方法会创建许多临时对象,并会导致大量垃圾回收。 However, Serializable interface is easier to implement. 但是,可Serializable接口更易于实现。

Look at the example below (Serializable): 请看下面的示例(可序列化):

// MyObjects Serializable classimport java.io.Serializable;import java.util.ArrayList;import java.util.TreeMap;import android.os.Parcel;import android.os.Parcelable;public class MyObjects implements Serializable {    private String name;    private int age;    public ArrayList address;    public MyObjects(String name, int age, ArrayList address) {        super();        this.name = name;        this.age = age;        this.address = address;    }    public ArrayList getAddress() {        if (!(address == null))            return address;        else            return new ArrayList();    }    public String getName() {        return name;    }    public String getAge() {        return age;    }}
// MyObjects instanceMyObjects mObjects = new MyObjects("name", "age", "Address array here");// Passing MyObjects instance via intentIntent mIntent = new Intent(FromActivity.this, ToActivity.class);mIntent.putExtra("UniqueKey", mObjects);startActivity(mIntent);
// Getting MyObjects instanceIntent mIntent = getIntent();MyObjects workorder = (MyObjects)    mIntent.getSerializableExtra("UniqueKey");

Parcelable 可包裹

Parcelable process is much faster than Serializable . Parcelable过程比可Serializable要快得多。 One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. 原因之一是我们对序列化过程很明确,而不是使用反射来推断它。 It also stands to reason that the code has been heavily optimized for this purpose. 也有理由为此目的对代码进行了优化。

Look at the example below (Parcelable): 请看下面的示例(可拆分):

// MyObjects Parcelable classimport java.util.ArrayList;import android.os.Parcel;import android.os.Parcelable;public class MyObjects implements Parcelable {    private int age;    private String name;    private ArrayList address;    public MyObjects(String name, int age, ArrayList address) {        this.name = name;        this.age = age;        this.address = address;    }    public MyObjects(Parcel source) {        age = source.readInt();        name = source.readString();        address = source.createStringArrayList();    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeInt(age);        dest.writeString(name);        dest.writeStringList(address);    }    public int getAge() {        return age;    }    public String getName() {        return name;    }    public ArrayList getAddress() {        if (!(address == null))            return address;        else            return new ArrayList();    }    public static final Creator CREATOR = new Creator() {        @Override        public MyObjects[] newArray(int size) {            return new MyObjects[size];        }        @Override        public MyObjects createFromParcel(Parcel source) {            return new MyObjects(source);        }    };}
// MyObjects instanceMyObjects mObjects = new MyObjects("name", "age", "Address array here");// Passing MyOjects instanceIntent mIntent = new Intent(FromActivity.this, ToActivity.class);mIntent.putExtra("UniqueKey", mObjects);startActivity(mIntent);
// Getting MyObjects instanceIntent mIntent = getIntent();MyObjects workorder = (MyObjects) mIntent.getParcelableExtra("UniqueKey");

You can pass ArrayList of Parcelable objects as below: 您可以按如下所示传递Parcelable对象的ArrayList

// Array of MyObjectsArrayList mUsers;// Passing MyOjects instanceIntent mIntent = new Intent(FromActivity.this, ToActivity.class);mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);startActivity(mIntent);
// Getting MyObjects instanceIntent mIntent = getIntent();ArrayList mUsers = mIntent.getParcelableArrayList("UniqueKey");

Conclusion 结论

  1. Parcelable is faster than Serializable interface Parcelable比可Serializable接口快
  2. Parcelable interface takes more time to implement compared to Serializable interface 与可Serializable接口相比,可Parcelable接口需要花费更多的时间来实现
  3. Serializable interface is easier to implement Serializable接口更易于实现
  4. Serializable interface creates a lot of temporary objects and causes quite a bit of garbage collection Serializable接口会创建许多临时对象,并导致大量垃圾回收
  5. Parcelable array can be passed via Intent in android Parcelable数组可以在Android中通过Intent传递

#5楼

Parcelable much faster than serializable with Binder, because serializable use reflection and cause many GC. 与使用Binder进行序列化相比,可包裹性要快得多,因为可序列化使用反射并导致许多GC。 Parcelable is design to optimize to pass object. 可打包设计旨在优化传递对象。

Here's link to reference. 这里是参考链接。 http://www.developerphil.com/parcelable-vs-serializable/ http://www.developerphil.com/parcelable-vs-serializable/


#6楼

I'm actually going to be the one guy advocating for the Serializable. 我实际上将成为提倡Serializable的那个人。 The speed difference is not so drastic any more since the devices are far better than several years ago and also there are other, more subtle differences. 速度差异不再那么剧烈,因为这些设备比几年前要好得多,并且还存在其他更细微的差异。 See my blog post on the issue for more info. 有关更多信息,请参阅我的博客文章。

更多相关文章

  1. Android Interface Definition Language (AIDL) android接口定义
  2. Android布局优化(四)X2C — 提升布局加载速度200%
  3. Android 多线程之synchronized锁住的是代码还是对象(二)
  4. Android重量级开发之--提高android启动速度研究
  5. Android Handler机制5之Message简介与消息对象对象池
  6. android 编译错误--超出接口数量限制
  7. Android: Android 3.0 SDK发布,速度更新之

随机推荐

  1. Linux中断(interrupt)子系统之三:中断流控处
  2. 我应该在哪里添加Yocto位烤任务来创建工
  3. linux常用的有关网络操作的命令:
  4. linux的0号进程和1号进程
  5. Linux 驱动面试题总结
  6. Linux命令之find(一)
  7. 从Android应用访问Chromebook的localhost
  8. MeeGo定位跨4大平台OS Novell主攻平板电
  9. linux下安装nginx,支持rewrite、ssl
  10. 在linux 列出 超级用户 普通用户和 系统