This paper mainly introduces the function, efficiency, difference and choice of parcelable and Serializable, and introduces the advanced knowledge of Java serialization in the introduction of serializable.
2. Use
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, designed to transmit data efficiently between different components within the program and between different Android programs (AIDL), which only exists in memory, Parcelable is the carrier of the message through IBinder communication.
From the above design we can see the pros and cons.
2. Efficiency and Choice
Parcelable performance is better than serializable, in terms of memory overhead, so it is recommended to use parcelable when transferring data between memory, such as transfer between activity, and serializable can persist data for easy preservation. So choose serializable when you need to save or transfer data from the network, because Android versions 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). Parcelable, however, needs to implement Writetoparcel, describecontents functions, and static creator variables, essentially defining how to package and reconcile the work itself, and the serialization of these operations is done entirely by the underlying.
An example of an implementation of parcelable is as follows
1 Public classMyparcelableImplementsparcelable {2 Private intMdata; 3 PrivateString MSTR; 4 5 Public intdescribecontents () {6 return0; 7 } 8 9 //Write data to saveTen Public voidWritetoparcel (Parcel out,intflags) { One Out.writeint (Mdata); A out.writestring (MSTR); - } - the //object used to create a custom parcelable - Public Static FinalParcelable.creator<myparcelable>CREATOR -=NewParcelable.creator<myparcelable>() { - Publicmyparcelable Createfromparcel (Parcel in) { + return Newmyparcelable (in); - } + A PublicMyparcelable[] NewArray (intsize) { at return NewMyparcelable[size]; - } - }; - - //read data for recovery - Privatemyparcelable (Parcel in) { inMdata =In.readint (); -MSTR =in.readstring (); to } +}
From the above we can see that the write and readout order of 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.
[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 partial fields, or you can override WriteObject, ReadObject methods to customize the serialization process
Other:
Android.os.BadParcelableException:ClassNotFoundException when unmarshalling
Reference:
Http://developer.android.com/reference/android/os/Parcelable.html
Go The difference between Android parcelable and serializable