What is parcelable?
Parcelable, defines the interface that writes data to parcel, and reads from parcel. An entity (represented by a class), if it needs to be encapsulated in a message, the interface must be implemented and the interface is implemented, and the entity becomes "packaged".
Parcelable Passing objects
There are two main ways to serialize an Android object:
1. Implement serializable interface, implement serializable interface is javase itself support;
2. Implement the Parcelable interface, Parcelable is a unique feature of Android, the efficiency is higher than the implementation of serializable interface, like for intent data transmission also support, but also can be used in interprocess communication (IPC),
In addition to the basic types, only classes that implement the Parcelable interface can be placed in parcel.
Parcelable Interface Definition
Public interface Parcelable {//Content Description interface, basic without tube public int describecontents (); Write interface function, package public void Writetoparcel (Parcel dest, int flags); Read interface, the purpose is to construct an instance processing of the class that implements the parcelable from the parcel. Because the implementation class is still unknown here, it is necessary to use the template's way of inheriting the class name passed through the template parameter. To enable the introduction of template parameters, the creator embedded interface is defined here, with two interface functions returning individual and multiple instances of inheriting classes. Public interface Creator<t> {public T createfromparcel (Parcel source); Public t[] NewArray (int size); }
How to implement Parcelable interface?
From the Parcelable interface definition, we can see that implementing the Parcelable interface requires us to implement the following methods:
1.describeContents method. Content interface Description, default return 0 can be;
2.writeToParcel method. This method writes the data for the class to an externally supplied parcel. That is, packaging the data to be passed to the parcel container to save the data from the parcel container, which is declared as follows:
Writetoparcel (Parcel dest, int flags) The meaning of the specific parameters see Javadoc
3. Static Parcelable.creator interface, this interface has two methods:
Createfromparcel (Parcel in) reads the passed data value from the Parcel container and encapsulates it as a Parcelable object to return to the logical layer.
NewArray (int size) creates an array of type T, length size, and only one sentence (return new t[size]). method is used by the outer class to deserialize the array of this class.
Code implementation
-
Encapsulates the data and passes the person object implementing the Parcelable interface to the twoactivity;
Public class demoactivity extends activity { /** called when the activity is first created. */ @Override public void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (r.layout.main); // package data person p = new person (); p.setid (1); p.setname ("xiaoming"); // passing person objects with intent intent i = new intent (This, twoactivity.class); i.putextrA ("person", p); startactivity (i); } }
2.TwoActivity gets the data, from the demoactivity passed the person object to parse and print;
public class Twoactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {// TODO auto-generated Method Stub super.oncreate (savedinstancestate); Person P = (person) getintent (). Getparcelableextra ("person"); System.out.println ("p_id" +p.getid ()); System.out.println ("P_name" +p.getname ()); }}
Implementation of the 3.parcelable interface
public class person implements parcelable{ Member Variables private int id; private String Name; // 1. You must implement the Parcelable.creator interface, or you will get an error when retrieving the person data. The following: // android.os.badparcelableexception: // parcelable protocol requires a parcelable.creator object called Creator on class com.um.demo.person // 2. This interface implements the reading of the person data from the Percel container. and returns the person object to the logical layer using &NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;3. Implementing the Parcelable.creator interface object name must be creator, as well as the error mentioned above; // 4. In reading the data in the parcel container, the data must be read in the order in which the member variable is declared, otherwise there will be an error // 5 getting the data. Deserialization Object public static final parcelable.creator<person> creator = new creator() { @Override Public person createfromparcel (Parcel source) { // TODO Auto-generated method stub // must read the data in the order in which the member variable is declared, or there will be an error getting data person p = new person (); p.setid (Source.readint ()); p.setname (Source.readstring ()); return p; } @Override public person[] newarray(int size) { // todo Auto-generated method stub return new Person[size]; } }; public int getid () { return id; } public void setid (Int id) { this.id = id; } public string getname () { return name; } public void setname (String name) { this.name = name; } @Override public int describecontents () { // TODO Auto-generated method stub return 0; } @Override public void writetoparcel (parcel dest, int flags) { // TODO Auto-generated method stub // 1. You must encapsulate the data in the order in which the member variable is declared, or there will be an error getting data // 2. Serializing an object dest.writeint (ID); dest.writestring (name); }}
(Android) Use of the Parcelable interface