Parcel is a lightweight object serialization (similar to serierlizable). It can be used for inter-process communication. When used, a static creator must be written, and the data written and read sequence must be consistent.
Here is an example of stackoverflow.
We have studied how to transmit the list <t>. The following example transfers the arraylist directly <string>:
Public class myparcelinfo implements parcelable {private int feildcount; private arraylist <string> feildnamelist; Public myparcelinfo (parcel in) {feildcount = in. readint (); feildnamelist = new arraylist <string> (); In. readlist (feildnamelist, classloader. getsystemclassloader ();} public myparcelinfo () {} public int describecontents () {return 0;} public void writetoparcel (parcel DEST, int flags) {DeST. writeint (feildcount); DeST. writelist (feildnamelist);} public static final parcelable. creator <myparcelinfo> creator = new parcelable. creator <myparcelinfo> () {public myparcelinfo createfromparcel (parcel in) {return New myparcelinfo (in);} public myparcelinfo [] newarray (INT size) {return New myparcelinfo [size] ;}}; public void setfeildcount (INT count) {This. feildcount = count;} public int getfeildcount () {return this. feildcount;} public arraylist <string> getfeildlist () {return this. feildnamelist;} public void setfeildlist (arraylist <string> List) {This. feildnamelist = List;} is sent belowCode : Protected void sendparcel (){ Myparcelinfo info = new myparcelinfo (); info. setfeildcount (4); arraylist <string> List = new arraylist <string> (); list. add ("lisp"); list. add ("JNI"); list. add ("Net"); list. add ("box2d"); info. setfeildlist (list); intent it = new intent (mainactivity. this, parcelreceiveractivity. class); it. putextra ("info", Info); this. startactivity (it );}
The following is the receiving code:
Myparcelinfo info = This. getintent (). getparcelableextra ("info"); arraylist <string> infolist = info. getfeildlist ();
Like string, the following types can be transferred:
-
- Null
-
- String
-
- Byte
- Short
-
- Integer
-
- Long
-
- Float
-
- Double
-
- Boolean
-
- String []
-
- Boolean []
-
- Byte []
-
- Int []
-
- Long []
-
- Object [] (supporting objects of the same type defined here ).
-
Bundle
-
- Map (as supported
Writemap
).
-
- Any object that implements
Parcelable
Protocol.
-
- Parcelable []
-
- Charsequence (as supported
Textutils. writetoparcel
).
-
- List (as supported
Writelist
).
-
Sparsearray
(As supportedWritesparsearray
).
-
Ibinder
- Any object that implements serializable (but see
Writeserializable
For caveats). Note that all of the previous types have relatively efficient implementations for writing to a parcel; having to rely on the generic serialization
Approach is much less efficient and shoshould be avoided whenever possible.
Note: If you want to transmit other custom objects, this object must be implements parcelable.
Parcel can be used to send objects between activities. Compared with Bundle transfer, parcel can package data into objects for transmission. It is more readable and maintainable than using bundle to transmit a large number of keys, value key-value pairs are good, and can also be extended to pass other types of objects (implements serializalbe or parcelable ).
The Application of inter-process communication needs to be further studied.