Android parcel Learning

Source: Internet
Author: User
Tags object serialization

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 supportedWritemap).
  • Any object that implementsParcelableProtocol.
  • Parcelable []
  • Charsequence (as supportedTextutils. writetoparcel).
  • List (as supportedWritelist).
  • Sparsearray(As supportedWritesparsearray).
  • Ibinder
  • Any object that implements serializable (but seeWriteserializableFor 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.

 
 

 
 
 
 
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.