Go The role of parcelable in Android

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_78e3ae430100pxba.html

Android offers a new type: Parcel. This class is used as a container for encapsulating data, and the encapsulated data can be passed through intent or IPC. Except for the base type, only classes that implement the Parcelable interface can be placed in parcel.  parcelable implementation essentials: Need to implement three things 1) Writetoparcel method. This method writes the data for the class to the externally supplied Parcel. The declaration is as follows: Writetoparcel (Parcel dest, int flags) The Javadoc2 method is the meaning of the specific parameters. Don't understand what's the use, anyway directly return 0 can also 3) static Parcelable.creator interface, this interface has two methods: Createfromparcel (Parcel in) implements the function of creating an instance of a class from in NewArray (int Size) Create an array of type T, length size, and only one sentence (return new t[size]). It is estimated that this method is used by the outer class to deserialize the array of this class. The receiving information of the test is 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) {          SUP Er.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 ());         }   sent 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) {          SUP Er.oncreate (savedinstancestate);           Setcontentview (R.layout.main);           Intent 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);      }   }  parcelable implementation class 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 () {        & nbsp return 0;      } &NBSP;&NBSP;&NBSP;&NBSP;&NBSp   @Override      public void Writetoparcel (Parcel dest, int flags) {      &N Bsp     DEST.WRITEMAP (map);           dest.writestring (name);      }       public static final parcelable.creator<person> Creator = new P Arcelable. 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    &nbsP     Public person[] NewArray (int size) {             //TODO Auto-gene Rated method stub               return null;          }      };     

}

Go The role of parcelable in Android

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.