An example of parcelable in Android _java

Source: Internet
Author: User
Tags int size

There is one type of Android available: Parcel. is used as a container for encapsulating data, and the encapsulated data can be passed through intent or IPC. In addition to the basic types, 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 of the class to the externally provided parcel. The declaration is as follows:
Writetoparcel (Parcel dest, int flags) specific parameter meanings see Javadoc

2) Describecontents method. don't understand what's the use, anyway directly return 0 also can

3 Static Parcelable.creator interface. This interface has two methods:

Createfromparcel (Parcel in) implements the ability to create an instance of a class from in

NewArray (int size) creates an array of type T with a length of size, only one sentence (return new t[size]). This method is estimated to be used by an external class to deserialize the array of this class.

Test Receive information activity:

Import 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 ());  
  }  
 

Activity sent:

Import 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 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 classes for 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&G T  
    
  ();  
  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 Creato  
      R @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.