1. parcelable Interface
Interface for classes whose instances can is written to and restored from a Parcel. Classes implementing the Parcelable interface must also has a static field called CREATOR, which is an object Implementin G The Parcelable.creator interface.
2. the implementation of parcelable is for serialization, so why Serialize?
1) Permanently save the object, save the object's byte sequence to the local file;
2) passing objects through a serialized object in the network;
3) Passing objects between processes through serialization.
3. ways to implement serialization
There are two options for implementing serialization in Android: One is to implement the Serializable interface (which is supported by the javase itself), the first is to implement the Parcelable interface (Android-specific features, efficiency than the implementation of serializable interface efficient, Can be used for intent data transfer, or for interprocess communication (IPC). The implementation of the serializable interface is very simple, declare it, and the implementation of the Parcelable interface is slightly more complex, but more efficient, it is recommended that this method to improve performance.
Note: There are two ways to pass objects intent in Android: one is bundle.putserializable (Key,object) and the other is Bundle.putparcelable (Key,object). Of course, these objects have certain conditions, the former is the implementation of the serializable interface, and the latter is the implementation of the Parcelable interface.
4. principles for choosing a serialization method
1) When using memory, parcelable performance is higher than serializable, so we recommend using Parcelable.
2) serializable generates a large number of temporary variables at serialization time, resulting in frequent GC.
3) parcelable can not be used in the case of storing data on disk, because Parcelable does not guarantee the continuity of the data in the event of external change. Although serializable is inefficient, it is recommended to use serializable at this point.
5. Application Scenarios
It is necessary to pass some data between multiple parts (activity or service) through intent, and simple types (such as numbers, strings) can be placed directly into intent. Complex types must implement the Parcelable interface.
6 , parcelable Interface Definition
Public Interfaceparcelable {//content Description interface, basic no tube Public intdescribecontents (); //Write interface functions, packaging Public voidWritetoparcel (Parcel dest,intflags); //read interface, the purpose is to construct an instance processing of the class that implements the parcelable from the parcel. Because the implementation class is still unknown here, it is necessary to use the template to inherit the class name passed through the template parameter//To enable the introduction of template parameters, the creator embedded interface is defined here, containing two interface functions to return individual and multiple instances of inheriting classes, respectively Public InterfaceCreator<t> { PublicT Createfromparcel (Parcel source); PublicT[] NewArray (intsize); }}
7 , achieve parcelable Steps
1) Implements Parcelable
2) Rewrite the Writetoparcel method to serialize your object to a parcel object, that is, to write the data of the class to an externally supplied parcel, to package the data that needs to be passed to the parcel container to save the data from the parcel container
3) Rewrite the Describecontents method, the Content interface description, the default return 0 can be
4) Instantiate the static internal object Creator Implement Interface Parcelable.creator
Public static final parcelable.creator<t> Creator
Note: the public static final one can not be less, the internal object creator name can not be changed, must all uppercase. The two methods in this interface need to be rewritten: the Createfromparcel (Parcel in) Implementation reads the passed data value from the Parcel container, encapsulates the Parcelable object to return the logical layer, and newarray (int size) creates a type of T, An array of size length, with just one sentence (return new t[size]), for the outer class to deserialize the array of this class.
In short: Map your objects into parcel objects through writetoparcel, and then map parcel objects to your objects through Createfromparcel. It is also possible to think of parcel as a stream through which the object is written to the stream through the Writetoparcel, and the object is read from the stream through the Createfromparcel, but this process needs to be done, so the order of writing and the order of reading must be consistent.
The code is as follows:
Public classMyparcelable implements parcelable {Private intMdata; Public intdescribecontents () {return 0; } Public voidWritetoparcel (Parcel out,intflags) { out. Writeint (Mdata); } Public StaticFinal parcelable.creator<myparcelable> Creator =NewParcelable.creator<myparcelable>() { PublicMyparcelable Createfromparcel (Parcelinch) { return NewMyparcelable (inch); } PublicMyparcelable[] NewArray (intsize) { return NewMyparcelable[size]; } }; PrivateMyparcelable (Parcelinch) {Mdata=inch. ReadInt (); } }
8 , Serializable implementation and Parcelabel the difference between implementations
1) Serializable implementation, only need implements Serializable can. This simply marks the object and the system automatically serializes it.
2) Parcelabel implementation, not only need to implements Parcelabel, but also to add a static member variable Creator in the class, this variable needs to implement the Parcelable.creator interface.
Comparison of the two codes:
1 ) to create Person class, Implementing Serializable
Public classPerson implements serializable{Private StaticFinalLongSerialversionuid =-7060210544600464481L; PrivateString name; Private intAge ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }}
2 ) to create Book class, Implementing parcelable
Public classBook implements parcelable{PrivateString BookName; PrivateString author; Private intpublishdate; PublicBook () {} PublicString Getbookname () {returnBookName; } Public voidsetbookname (String bookname) { This. BookName =BookName; } PublicString Getauthor () {returnauthor; } Public voidSetauthor (String author) { This. Author =author; } Public intgetpublishdate () {returnpublishdate; } Public voidSetpublishdate (intpublishdate) { This. publishdate =publishdate; } @Override Public intdescribecontents () {return 0; } @Override Public voidWritetoparcel (Parcel out,intflags) { out. WriteString (BookName); out. WriteString (author); out. Writeint (publishdate); } Public StaticFinal parcelable.creator<book> Creator =NewCreator<book>() {@Override PublicBook[] NewArray (intsize) { return NewBook[size]; } @Override PublicBook Createfromparcel (Parcelinch) { return NewBook (inch); } }; PublicBook (Parcelinch) {BookName=inch. readString (); Author=inch. readString (); Publishdate=inch. ReadInt (); }}
Parcelable interface usage in Android