Similarities and differences of Android serialized serializable and Parcelable

Source: Internet
Author: User
Tags int size serialization

Both are used to support serialization, deserialization operation, the biggest difference is the storage medium, serializable use IO Read and write memory on the hard disk, and parcelable is directly read and write in RAM, it is obvious that the memory read and write speed is usually greater than IO read and write, So in Android it's usually preferred to choose parcelable.

Serializable is not the focus of the current focus, but you can view the Java serialization algorithm dialysis in this article to implement a simple serializable example, look at the serialized generated IO file, and in 16 read and explain the meaning of each of the 16 digits.


1, the role

Serializable's role is to save object properties to local files, databases, network streams, RMI to facilitate data transmission, of course, this transmission can be within the program or can be two between programs. The Android Parcelable was designed to be designed for efficient transfer of data between different components within the program and between different Android programs (AIDL), which only existed in memory, because of the serializable efficiency. Parcelable is the carrier of messages that are communicated via IBinder.

From the above design we can see the pros and cons.


2. Efficiency and Selection

Parcelable performance is better than serializable, small in memory overhead, so it is recommended to use parcelable when data is transferred between the two, such as the activity between the activity, and the serializable can persist the data for easy storage. So choose serializable when you need to save or network to transfer data, because Android different version parcelable may be different, so it is not recommended to use parcelable for data persistence


3. Programming Realization

For serializable, the class only needs to implement the serializable interface and provide a serialized version ID (serialversionuid). Parcelable, however, needs to implement Writetoparcel, describecontents functions, and static creator variables, in effect defining how to package and reconcile the work itself, and the serialization of these operations is entirely implemented by the bottom.

One implementation example of Parcelable is as follows

public class Myparcelable implements Parcelable {
private int mdata;
Private String MSTR;

public int describecontents () {
return 0;
}

Write data for saving
public void Writetoparcel (Parcel out, int flags) {
Out.writeint (Mdata);
Out.writestring (MSTR);
}

Object used to create a custom parcelable
public static final Parcelable.creator<myparcelable> Creator
= new Parcelable.creator<myparcelable> () {
Public myparcelable Createfromparcel (Parcel in) {
Return to New myparcelable (in);
}

Public myparcelable[] NewArray (int size) {
return new Myparcelable[size];
}
};

Read data for Recovery
Private myparcelable (Parcel in) {
Mdata = In.readint ();
MSTR = In.readstring ();
}
}

From the above we can see that parcel write and read out the order is consistent. If the element is a list read out need to first new one ArrayList incoming, otherwise will report null pointer exception. As follows:

List = new arraylist<string> ();
In.readstringlist (list);

PS: In their own use, read data mistakenly put the front int data as long read out, the result of the order of the following disorder, reported as follows, when more than the class field must keep write and read the type and the same order.

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 function

Serializable serialization does not save static variables, you can use the Transient keyword to not serialize some fields, or you can override WriteObject, ReadObject methods to implement serialization process customization

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.