Chapter II Use of--parcelable interfaces (cross-process, intent transmission)

Source: Internet
Author: User
Tags int size serialization

First, Parcelable class (Android Exclusive)

Summary : parcelable is an interface.

role : Is the serialization interface provided by Android, which implements serialization and deserialization operations.

Second, cross-process use

Step One: Create the Book Class inheritance Parcelable interface

 Public classBook implements parcelable {PrivateString Mbookname; Private intMbookid;/** * Prepare: Create book class and inherit Parcelable interface*/     PublicBook (intbookId, String bookname) {Mbookid=bookId; Mbookname=BookName; } @Override PublicString toString () {returnMbookid+""+Mbookname; }} 
View Code

Step Two: A method that prompts you to override the interface

Describecontents (): Returns the element content of the current object, returns 1 if it contains a file descriptor (what is called a file descriptor), or returns 0, which generally returns 0 (so don't worry about it).

Writetoparcel (Parcel out,int Flags): Writing objects to serialization

Parcel out: A system-supplied output stream that stores member variables in memory.

int flags:0 or 1, 1 means that the current object needs to be saved as a return value (not understood), basically all cases are 0, (so don't worry about it)

// connect the code above    @Override        publicint  describecontents () {          return 0;        }  // description file, now just return 0 on line     @Override         publicvoid int flags) {             dest.writeint (mbookid);         Dest.writestring (Mbookname);    } // writes the current object to the serialization structure

Step three: Create a Deserialization object Parcelable.creator<t> interface: specifically for deserialization

Override this interface's method:

Createfromparcel (Parcel in): A system-supplied input stream that obtains data from a serialized object.

NewArray (int size): Creates an array of the object (temporarily feeling useless)

Note: When deserializing, get the data in the order in which it was serialized into the data, otherwise you will not receive the value.

//you must write public static final parcelable.creator<book> in this format Creator   Public Static FinalParcelable.creator<book> Creator =NewParcelable.creator<book>() {@Override PublicBook Createfromparcel (Parcel source) {return NewBook (source); }//get input stream, deserialize object@Override PublicBook[] NewArray (intsize) {             return NewBook[0]; } };//Creating a construction method, instantiating an object PrivateBook (Parcel in) {Mbookid=In.readint (); Mbookname=in.readstring ();}

3. Principle

Parcelable uses parcel out to store data in memory and then obtains data from memory through parcel in.

Third, the transfer of parcelable class between intent (that is, the method of each activity Transfer object)

Depending on how you created the Parcelable, create the class, and then apply the intent transfer.

Chapter II Use of--parcelable interfaces (cross-process, intent transmission)

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.