This article illustrates the parcel mechanism of Android development. Share to everyone for your reference. The specific analysis is as follows:
In Java, there is a serialization mechanism. But on the Android device, the new serialization mechanism was designed because of limited memory.
Container for a message (data and object references) that can is sent through an ibinder. A Parcel can contain both flattened data that would be unflattened on the other side of the IPC (using the various methods Here's for writing specific types, or the Generalparcelable interface), and references to liveibinder objects of that would resu Lt 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 APIs for placing arbitrary objects to a Parcel) is designed as a high-perform ance IPC Transport. As such, it is isn't appropriate to place any Parcel data in to persistent storage:changes in the underlying of any of the data in the Parcel can render older data unreadable.
As you can see from the official explanation above, parcel is primarily used for serialization, encoding at one end, and decoding at the other end.
Essentially, it's a serialize, but it's serialized and deserialized in memory, using contiguous memory space, and therefore more efficient.
What we're going to say next is how the parcel class is applied. In the case of an application, the most common scenario for using the parcel class is to pass data between the activity. Yes, when using intent to pass data between the activity, the complex objects can be passed through the parcelable mechanism.
Specific examples can be found here, well written.
When implementing the Parcelable interface, you must implement two of these methods and define a 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 information about the class object to serialization.
There are two functions 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 an object that has already been serialized. The effect of NewArray method is unknown. The Createfromparcel method for creator members that are 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 tells the platform how to store instances of the class in the package. With this convention, the platform knows how to serialize and deserialize.
I hope this article will help you with your Android program.