为什么Android为序列化对象提供了两个接口?序列化对象与Android Binder和AIDL文件interopt ?


当前回答

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. However, in most cases, the slowness of Serializable won’t be noticeable. 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.

来源:http://www.developerphil.com/parcelable-vs-serializable/

其他回答

在Parcelable中,开发人员编写用于编组和反编组的自定义代码,因此与Serialization相比,它创建的垃圾对象更少。由于这个自定义实现,Parcelable over Serialization的性能显著提高(大约快两倍)。

Serializable是一个标记接口,这意味着用户不能根据自己的需求编组数据。在序列化中,使用Java反射API在Java虚拟机(JVM)上执行编组操作。这有助于识别Java对象的成员和行为,但最终也会创建大量垃圾对象。因此,与Parcelable相比,Serialization进程较慢。

编组和反编组的含义是什么?

简而言之,“编组”是指将数据或对象转换为字节流的过程,而“解组”是将字节流转换回原始数据或对象的相反过程。转换是通过“序列化”实现的。

http://www.jguru.com/faq/view.jsp?EID=560072

你可以在intent中使用serializable对象,但是在使serialize成为Parcelable对象的时候,它会给出一个严重的异常,比如NotSerializableException。不建议使用serializable和Parcelable。所以最好是扩展Parcelable与你想要使用的对象捆绑和意图。由于这个Parcelable是android特定的,所以它没有任何副作用。 :)

打包比使用Binder序列化快得多,因为序列化使用反射并导致许多GC。Parcelable是为了优化传递对象而设计的。

这是参考链接。 http://www.developerphil.com/parcelable-vs-serializable/

在编组和解组方面存在一些性能问题。Parcelable比Serializable快两倍。

请浏览以下连结:

http://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development

Serializable接口可以像Parcelable接口一样使用,从而获得(不是很多)更好的性能。 只需覆盖这两个方法来处理手动编组和解组过程:

private void writeObject(java.io.ObjectOutputStream out)
    throws IOException
private void readObject(java.io.ObjectInputStream in)
    throws IOException, ClassNotFoundException

不过,在我看来,在开发原生Android时,使用Android api是正确的选择。

看到的:

https://bitbucket.org/afrishman/androidserializationtest/ https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html