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


当前回答

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

其他回答

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

请浏览以下连结:

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

可序列化的

Serializable是一个可标记的接口,也可以作为空接口调用。它没有任何预先实现的方法。Serializable将对象转换为字节流。因此用户可以在一个活动之间传递数据到另一个活动。可序列化的主要优点是创建和传递数据非常容易,但与可打包相比,它是一个缓慢的过程。

Parcelable

可打包的比可序列化的快。Parcel able将对象转换为字节流,并在两个活动之间传递数据。与序列化相比,编写可包的代码有点复杂。在两个活动之间传递数据时,它不会创建更多的临时对象。

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是Android开发中的一种标准。但不是因为速度

Parcelable是数据传输的推荐方法。但是如果你正确地使用serializable,你会发现serializable有时甚至比parcelable还要快。或者至少时间是可以比较的。

Parcelable比Serializable快吗?

不,如果序列化操作正确的话。

Usual Java serialization on an average Android device (if done right *) is about 3.6 times faster than Parcelable for writes and about 1.6 times faster for reads. Also it proves that Java Serialization (if done right) is fast storage mechanism that gives acceptable results even with relatively large object graphs of 11000 objects with 10 fields each. * The sidenote is that usually everybody who blindly states that "Parcelable is mush faster" compares it to default automatic serialization, which uses much reflection inside. This is unfair comparison, because Parcelable uses manual (and very complicated) procedure of writing data to the stream. What is usually not mentioned is that standard Java Serializable according to the docs can also be done in a manual way, using writeObject() and readObject() methods. For more info see JavaDocs. This is how it should be done for the best performance.

所以,如果serializable更快更容易实现,为什么android有parcelable呢?

原因在于本地代码。创建Parcelable不仅仅是为了进程间通信。它也可以用于代码间通信。你可以从c++原生层发送和接收对象。就是这样。

你应该选择什么?两者都很有效。但我认为Parcelable是更好的选择,因为谷歌推荐它,你可以从这个帖子中看到,它更受欢迎。

如果你在android studio中使用paracelable插件,parcelable的实现可以更快。搜索Android Parcelable代码生成器