Functions of parcelable in Android

Source: Internet
Author: User

From: http://blog.sina.com.cn/s/blog_78e3ae430100pxba.html

Android provides a new type: parcel. This class is used as a container to encapsulate data. The encapsulated data can be transmitted through intent or IPC. Except for the basic type, only classes that implement the parcelable interface can be put into parcel. Parcelable Implementation points: three things need to be implemented. 1) The writetoparcel method. This method writes the data of the class to the external provided parcel. The declaration is as follows: for specific parameter meanings of writetoparcel (parcel DEST, int flags), see javadoc2) describecontents method. I don't understand what it is, but it can return 0 directly anyway. 3) Static parcelable. creator interface. This interface has two methods: createfromparcel (parcel in) to create a class instance from the in function newarray (INT size) to create a type of T, return New T [size. It is estimated that this method is used for external class deserialization of this class array. Activityimport android. app. activity; import android. content. intent; import android. OS. bundle; import android. OS. parcelable; public class test extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); intent I = getintent (); person P = I. getparcelableextra ("yes"); system. out. println ("---->" + P. name); system. out. println ("---->" + P. map. size () ;}} the activityimport Java. util. hashmap; import android. app. activity; import android. content. intent; import android. OS. bundle; public class testnew extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); intent = new intent (); person P = new person (); p. map = new hashmap <string, string> (); p. map. put ("yes", "Ido"); p. name = "OK"; intent. putextra ("yes", P); intent. setclass (this, test. class); startactivity (intent) ;}} implementation class of parcelable import Java. util. hashmap; import android. OS. parcel; import android. OS. parcelable; public class person implements parcelable {public hashmap <string, string> map = new hashmap <string, string> (); Public string name; @ override public int describecontents () {return 0 ;}@ override public void writetoparcel (parcel DEST, int flags) {DeST. writemap (MAP); DeST. writestring (name);} public static final parcelable. creator <person> creator = new parcelable. creator <person> () {// rewrite creator @ override public person createfromparcel (parcel source) {person P = new person (); p. map = source. readhashmap (hashmap. class. getclassloader (); p. name = source. readstring (); Return P ;}@ override public person [] newarray (INT size) {// todo auto-generated method stub return NULL ;}};

}

Related Article

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.