Understanding of Serializable and Parcelable
1. First of all, both of their interfaces are intended to realize the serialization of objects so that they can be passed. The so-called serialization is the process of replacing object information with a medium that can be stored.
2. Serializable is a serialized interface provided by jdk. This interface exists under the io package and can be used for input and output. It is very simple to use. Just let your class implement this interface. You can use the transient keyword to modify the attributes you don't want to serialize.
3. Parcelable is a serialized interface provided by SDK. It is troublesome to use the better one. After implementing this interface, you need to rewrite the writeToParcel method and write the properties that need to be serialized into Parcel;
Then CERATOR static member zodiac is also required to retrieve data from parcel. as follows
public static final Creator<Pen> CREATOR = new Creator<Pen>() { @Override public Pen createFromParcel(Parcel in) { return new Pen(in); } @Override public Pen[] newArray(int size) { return new Pen[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(color); dest.writeInt(size); }4. Both are used to support serialization and deserialization operations. The biggest difference between the two is the difference in storage media. Serializable uses IO reading and writing to store it on the hard disk, while Parcelable reads and writes directly in memory. It is obvious that the memory reading and writing speed is usually greater than IO reading and writing, so Parcelable is usually preferred in Android.
Through this article, I hope to help friends in need to thoroughly understand the knowledge of Java Serializable and Parcelable. Thank you for your support for this website!