Difference between Android Parcelable and Serializable, androidparcelable

Source: Internet
Author: User

[Switch] Android Parcelable and Serializable, androidparcelable

This article mainly introduces the functions, efficiency, difference and selection of Parcelable and Serializable. For details about Serializable, see the advanced understanding of Java serialization.

 

1. Role

Serializable is used to save the attributes of an object to a local file, database, network stream, or rmi for data transmission. Of course, this transmission can be in a program or between two programs. The Android Parcelable was originally designed because the Serializable efficiency was too slow. It was designed to efficiently transmit data between different components in the program and between different Android programs (AIDL, this data only exists in the memory, and Parcelable is the carrier of messages that communicate with IBinder.

From the above design, we can see the advantages and disadvantages.

 

2. Efficiency and selection

Parcelable has better performance than Serializable and has lower memory overhead. Therefore, Parcelable is recommended for data transmission between memories, such as data transmission between activities. Serializable can save data persistently and conveniently, so select Serializable when you need to save or transmit data over the network. Because Parcelable of different android versions may be different, we do not recommend using Parcelable for data persistence.

 

3. Programming

For Serializable, the class only needs to implement the Serializable interface and provide a serialVersionUID. Parcelable needs to implement writeToParcel, describeContents functions, and static CREATOR variables. In fact, it is to define how to package and package, and the serialization operations are fully implemented by the underlying layer.

An example of Parcelable implementation is as follows:

1 public class MyParcelable implements Parcelable {2 private int mData; 3 private String mStr; 4 5 public int describeContents () {6 return 0; 7} 8 9 // write data to save 10 public void writeToParcel (Parcel out, int flags) {11 out. writeInt (mData); 12 out. writeString (mStr); 13} 14 15 // create a custom Parcelable object 16 public static final Parcelable. creator <MyParcelable> CREATOR 17 = new Parcelable. creator <MyParcelable> () {18 public MyParcelable createFromParcel (Parcel in) {19 return new MyParcelable (in); 20} 21 22 public MyParcelable [] newArray (int size) {23 return new MyParcelable [size]; 24} 25}; 26 27 // read data recovery 28 private MyParcelable (Parcel in) {29 mData = in. readInt (); 30 mStr = in. readString (); 31} 32}

 

 

From the above we can see that the write and read order of Parcel is the same. If the element is read by list, a new ArrayList must be input first. Otherwise, a null pointer exception is reported. As follows:

list = new ArrayList<String>();in.readStringList(list);

PS: when reading data, the previous int data is mistakenly read as long. The order following the result is disordered and the following exception is reported, when there are many class fields, make sure to keep the type and sequence of writing and reading consistent.

11-21 20:14:10.317: E/AndroidRuntime(21114): Caused by: java.lang.RuntimeException: Parcel 
android.os.Parcel@4126ed60: Unmarshalling unknown type code 3014773 at offset 164

 

4. Advanced functions

Serializable serialization does not save static variables. You can use the Transient keyword to not serialize some fields, or override the writeObject and readObject methods to customize the serialization process.

 

Others:

Android. OS. BadParcelableException: ClassNotFoundException when unmarshalling

 

Refer:

Http://developer.android.com/reference/android/ OS /Parcelable.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.