let's introduce to you today The roles, efficiencies, distinctions and choices of parcelable and Serializable, and the introduction to Serializable are advanced knowledge of Java serialization.
1. function
Serializable 's role is to save the object's properties to local files, databases, network streams,RMI for easy data transfer, which can be within a program or between two programs. the android parcelable is designed to be Serializable inefficient, in order to be in the program between different components and different Android programs (AIDL) is designed to transmit data efficiently, which exists only in memory, andParcelable is the carrier of messages that are communicated through IBinder .
2. Efficiency and Choice
Parcelable has better performance than Serializable, and is smaller in memory overhead, so it is recommended to use parcelable when transferring data between memory ,such as transfers between activity, Serializable can persist data for easy storage, so choose Serializable when you need to save or transfer data from the network , because android versions are different parcelable may be different, so it is not recommended to use Parcelable for data persistence
3, programming implementation
for Serializable, classes only need to implement the Serializable interface and provide a serialized version ID (serialversionuid) . the parcelable needs to implement Writetoparcel,describecontents functions, and static CREATOR variables. In fact, it is the work of packaging and unpacking to define themselves, and the serialization of these operations is entirely at the bottom of the implementation.
An example of an implementation of parcelable is as follows:
from the above we can see The write and read Order of the Parcel is consistent. a null pointer exception is reported if the element is a list read out that requires a new ArrayList to be passed in first. as follows:
List = new arraylist<string> ();
In.readstringlist (list);
PS: in their own use,the read data mistakenly the preceding int data as long read out, the result of the order of confusion, reported the following exception, when the class field is more important to maintain the type of write and read and the order of the same.
11-21 20:14:10.317:e/androidruntime (21114): caused By:java.lang.RuntimeException:Parcel
[Email protected]: unmarshalling unknown type code 3014773 at offset 164
4, advanced functions on
Serializable serialization does not save static variables, you can use the Transient keyword to not serialize some fields, or you can overwrite writeobject,readobject method to implement the serialization process customization
Other:
Android.os.BadParcelableException:ClassNotFoundException when unmarshalling
It is also recommended APP a comprehensive test can be done at the end of the development: http://www.ineice.com/
The difference between Android parcelable and serializable