为什么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将对象转换为字节流,以便在Android的进程之间传递对象。

序列化将POJO转换为字符串(JSON字符串),并用于跨平台传输对象信息。

我的回答有点晚了,但我希望它能帮助到其他人。

在速度方面,可打包>序列化。但是,自定义Serializable是个例外。它几乎在Parcelable的范围内,甚至更快。

参考资料:https://www.geeksforgeeks.org/customized-serialization-and-deserialization-in-java/

例子:

要序列化的自定义类

class MySerialized implements Serializable { 

    String deviceAddress = "MyAndroid-04"; 

    transient String token = "AABCDS"; // sensitive information which I do not want to serialize

    private void writeObject(ObjectOutputStream oos) throws Exception {
        oos.defaultWriteObject();
        oos.writeObject("111111" + token); // Encrypted token to be serialized
    }

    private void readObject(ObjectInputStream ois) throws Exception {
        ois.defaultReadObject(); 
        token = ((String) ois.readObject()).subString(6);  // Decrypting token
    }

}

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/

可序列化的

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

Parcelable

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

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

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