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
;
}
}; }