An example analysis of Parcel mechanism developed by Android

  • 2020-06-19 11:43:44
  • OfStack

This paper describes the Parcel mechanism developed by Android. Share to everybody for everybody reference. The specific analysis is as follows:

In java, there is a serialization mechanism. However, on Android devices, due to limited memory, a new serialization mechanism has been designed.

Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the generalParcelable interface), and references to liveIBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.

Parcel is not a general-purpose serialization mechanism. This class (and the correspondingParcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

As you can see from the official explanation above, Parcel is mainly used for serialization, encoding on one side and decoding on the other.

Essentially, think of it as an Serialize, except that it is serialized and deserialized in memory, taking advantage of contiguous memory space, which makes it more efficient.

We are going to talk about how the Parcel class applies. In terms of applications, the most common scenario for using the Parcel class is passing data between Activity. Yes, when using Intent to pass data between Activity, you can pass complex objects through the Parcelable mechanism.

See here for an example, it's very well written.

When implementing the Parcelable interface, you must implement two of the methods and define an CREATOR:


@Override 
public int describeContents() {
    return 0; 
} 
@Override 
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(color); 
}

Where the writeToParcel method defines how to write the class object to the serialization.

Two functions are defined in the CREATOR object:


public MyColor createFromParcel(Parcel in) {
  return new MyColor(in);
}
public MyColor[] newArray(int size) {
  return new MyColor[size];
}

Where the createFromParcel method tells the platform how to build an instance of the class from a serialized object. The role of the newArray method is unclear. The createFromParcel method of the CREATOR member implemented in the Parcelable interface is used to tell the platform how to create an instance of the class from the package, while the writeToParcel method is used to tell the platform how to store an instance of the class in the package. With this convention, the platform knows how to serialize and deserialize.

I hope this article has been helpful for your Android programming.


Related articles: