From http://www.jiaojingkuan.com/archives/544. The explanation is thorough. Thanks
The serialization of custom objects in Android has two options: parcelable and 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 between processes through serialization.
2. for which to choose, refer to the following principles:
1. parcelable is superior to serializable in memory usage, so parcelable class 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.
Implementation:
1 serializable implementation, 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();
}
}