Understanding of Serializable and parcelable
1, the first of their two interfaces are to achieve the serialization of objects, so that it can be passed, the so-called serialization is to replace the object information can be stored in the process of media.
2, Serializable is the JDK provided by the serialization interface, the interface exists under the IO package, can be used for input and output, very simple to use, as long as your class to implement this interface OK; You can use the Transient keyword to modify attributes that you do not want to serialize.
3, Parcelable is the SDK provided by the serialization interface, the use of the more troublesome, the implementation of this interface, the need to rewrite the Writetoparcel method, will need to serialize the attributes written into the parcel;
You will then need to cerator the static member Zodiac to remove the data from the 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, deserialization operation, the biggest difference between the storage medium is different, serializable use IO Read and write stored on the hard disk, and parcelable is directly in memory read and write, it is obvious that the memory read and write speed is usually greater than IO read and write, So in Android it's usually preferred to choose parcelable.
Through this article hope to help the small partners need to thoroughly understand the knowledge of Java serializable and parcelable, thank you for your support to this site!