The difference between Android parcelable and serializable

Source: Internet
Author: User

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.

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, 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

  public class Myparcelable implements parcelable {private int mdata;             Private String MSTR;         public int describecontents () {return 0;            }//write data to save public void Writetoparcel (Parcel out, int flags) {out.writeint (mdata);        Out.writestring (MSTR);                }//used to create a custom Parcelable object public static final parcelable.creator<myparcelable> Creator                = new Parcelable.creator<myparcelable> () {public myparcelable createfromparcel (Parcel in) {            return new myparcelable (in);            } public myparcelable[] NewArray (int size) {return new myparcelable[size];                }        };            Read data to recover private myparcelable (Parcel in) {mdata = In.readint ();        MSTR = In.readstring ();   }   }

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.

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 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


The difference between Android parcelable and serializable

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.