Parcelable is an interface in which objects that implement the interface can be passed efficiently in the program. The object that is actually passed is parcel. According to the documentation, parcel is designed to be a high-performance IPC communication, not for persistent storage.
Realized before the feeling is quite complicated, today I feel as if it is very simple.
First, we write a book class that represents the data item.
Public classBook {PrivateString name; Private intISBN; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGETISBN () {returnISBN; } Public voidSETISBN (intISBN) { This. ISBN =ISBN; }}
Then let it implement the interface parcelable.
To implement this interface, two methods must be implemented: Describecontents,writetoparcel
Describecontents method can not how to tube, return to a 0 is done.
Writetoparcel method, look at the method name is to write data to the parcel object.
See below for the book class that implements the Parcelable interface.
Public classBookImplementsparcelable{PrivateString name; Private intISBN; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGETISBN () {returnISBN; } Public voidSETISBN (intISBN) { This. ISBN =ISBN; } @Override Public intdescribecontents () {return0; } @Override Public voidWritetoparcel (Parcel arg0,intarg1) {arg0.writestring (name); Arg0.writeint (ISBN); } }
The first parameter in the Writetoparcel method is the parcel object, and the Write* () method in the object is basically the basic data type.
By completing the above steps, you can pass the book object in parcelable form. For example, Putextra () in intent can use parcelable objects.
Here's how to use it:
New Book (); B.SETISBN(b.setname) ("ABCD"new Intent (); I.putextra ("book" = (book) I.getparcelableextra ("book"); LOG.I ("test", B2.getname () + "," + B2.GETISBN ());
In addition, an interface named Creator is defined in the Parcelable interface, and the comment says that a member variable (field) that must be created with a public name is creator.
There is a Createfromparcel () method, and Writetoparcel () is a reversible method.
Add the book class for this inner class as follows:
Public classBookImplementsparcelable{PrivateString name; Private intISBN; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGETISBN () {returnISBN; } Public voidSETISBN (intISBN) { This. ISBN =ISBN; } @Override Public intdescribecontents () {return0; } @Override Public voidWritetoparcel (Parcel arg0,intarg1) {arg0.writestring (name); Arg0.writeint (ISBN); } Public Static FinalParcelable.creator<book> Creator =NewParcelable.creator<book>() {@Override PublicBook Createfromparcel (Parcel arg0) {Book B=NewBook (); B.name=arg0.readstring (); B.ISBN=Arg0.readint (); returnb; } @Override PublicBook[] NewArray (intarg0) { return Newbook[arg0]; } }; }
Here's one thing to watch out for! The Order of the read* () and write* () operations using the Parcel object in the Createfromparcel () method and the Writetoparcel () method must be the same.
The above is the use of parcelable method.
Basic use of parcelable