Basic use of parcelable

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.