Differences between serializable and parcelable

Source: Internet
Author: User

Serialization method of custom objects in Android:

  • Implements parcelable
  • Implements serializable.
I. serialization reason:
    1. Permanently Save the object and save the object's byte sequence to a local file;
    2. Passing objects in the network by serializing objects;
    3. Objects are passed through serialization between processes.
2. for which to choose, refer to the following principles:
    1. When using memory, parcelable has higher performance than serializable. Therefore, parcelable is recommended.
    2. Serializable will generate a large number of temporary variables during serialization, resulting in frequent GC.
    3. Parcelable cannot be used when you want to store data on a disk, because parcelable cannot guarantee data continuity in the case of external changes. Although serializable is not recommended for low efficiency, we recommend that you use serializable in this case.
Three implementations:
    1. To implement serializable, you only need to inherit implements serializable. This only marks the object and the system will automatically serialize it.
    2. To implement parcelabel, you must add a static member variable creator to the class. This variable must inherit the parcelable. Creator interface.
public class MyParcelable implements Parcelable {     private int mData;     public int describeContents() {         return 0;     }     public void writeToParcel(Parcel out, int flags) {         out.writeInt(mData);     }     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];         }     };          private MyParcelable(Parcel in) {         mData = in.readInt();     } }

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.