go from blog: http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html 1. Realizing the function of serializationthe transfer of data is necessary in the application, but the normal data transfer only supports basic data types such as String, int, or higher data types, such as Arraylist,object. However, in the development, we often need to pass objects to achieve convenient data transmission purposes, which requires the realization of the object serialization. data transfer includes local data access, network data transmission, and intent data transmission. 2, the method of realizing serialization1) parcelable 2) Serializable 3, the use of the principle of serialization1) When using memory, parcelable efficiency is higher thanSerializable, so it is recommended to useparcelable. 2)serializable generates a large number of temporary variables at the same time that it is serialized, causing frequent GC. 3)parcelable cannot be used to save data on disk, so it is recommended to use theSerializable. 4, Serializableserializable implementation, only need to implementsserializable, the system will automatically serialize it 5, Parcelable1) implements parcelable 2) rewrite describecontents, Writetoparcel two methods, writetoparcel to map the current object to a parcel object @Overridepublic int describecontents () { //Content Description interface, return 0 by default return 0; } @OverridePublic void Writetoparcel (Parcel dest, int flags) {//Serializes your object into a parcel object, that is: writes the data of the class to an externally supplied parcel dest.writestring (ID); dest.writestring (path); dest.writestring (thumbnailpath); bitmap.writetoparcel (dest, 0); } 3) CreateParcelable.creatorobject, and rewrite the Createfromparcel, NewArray method;Createfromparcel used to map parcel objects to your objects Public static final parcelable.creator<imageitem> Creator = new parcelable.creator<imageitem> () { @OverridePublic Imageitem Createfromparcel (Parcel source) { Imageitem item = new Imageitem (); item.id = source.readstring (); Item.path = source.readstring (); Item.thumbnailpath = source.readstring (); Item.bitmap = Bitmap.CREATOR.createFromParcel (source); return item; } @OverridePublic imageitem[] NewArray (int size) {return new imageitem[size]; } }; so The usage of parcelable can be summarized as the implements Parcelable interface, and then add the Writetoparcel method that encapsulates your object as a parcel object, with a description of the describecontents, and then add the Parcel.creator object that maps the parcel object to your object, and override the mapping method. Precautions : 1) Writetoparcel and Createfromparcel two methods to add and remove variables in the same order 2) Bitmap The operation of the object, to use bitmap.writetoparcel (dest, 0) and Bitmap.CREATOR.createFromParcel (source) 3 ) The creation of the Parcel.creator remains unchanged Public static final parcelable.creator<imageitem> Creator = new Parcelable.creator<imageitem> () , it is best to replace only the <T> part 4) Bitmap transfer, size limit is 40KB5) Parcel Read and write to list: dest.writelist (photoitems) and source.readlist (This.photoitems, ImageItem.class.getClassLoader ()). The code is as follows: public list<imageitem> photoitems = new arraylist<imageitem> (); //parcel Read ConstructsPublic Imagefolder (Parcel source) { source.readlist (This.photoitems, ImageItem.class.getClassLoader ()); } @OverridePublic void Writetoparcel (Parcel dest, int flags) { dest.writelist (photoitems); } Public static final parcelable.creator<imagefolder> Creator = new creator<imagefolder> () { @Override Public imagefolder[] NewArray (int size) { return new imagefolder[size]; } @Override Public Imagefolder createfromparcel (Parcel source) { return new Imagefolder (source); } };With the above code, the list written to a custom object can be implemented by Writelist, and since the list itself is serialized, reading the list can be done byreadlist. The parameters of which readlist, Parameter 1: The list itself in the object, such as PhotoitemsThe ClassLoader of the object in the parameter 2:list, such asthe List<imageitem> photoitems,list object is Imageitem, so the parameter isImageItem.class.getClassLoader () Source : public class Imageitem implements Parcelable { /*** Image ID */public String ID; /*** Image Path */public String Path; /*** Image thumbnail path */Public String Thumbnailpath; /*** Image Bitmap */Public Bitmap Bitmap; /*** Get bitmap by Path * * @param reqwidth* @param reqheight* @return */Public Bitmap getbitmap (int reqwidth, int reqheight) {return Imageresizer.decodesampledbitmapfromfile (Path, Reqwidth,reqheight); }@Overridepublic int describecontents () {//Content Description interface, return 0 by defaultreturn 0; }@OverridePublic void Writetoparcel (Parcel dest, int flags) {//Serializes your object into a parcel object, namely: writes the data of the class to an externally supplied parcel, packages the data to be passed to the parcel container to save,//Parcel container for datadest.writestring (ID);dest.writestring (path);dest.writestring (thumbnailpath);bitmap.writetoparcel (dest, 0); } /*** Map the Parcle object to my object */Public static final parcelable.creator<imageitem> Creator = new parcelable.creator<imageitem> () {@OverridePublic Imageitem Createfromparcel (Parcel source) {Imageitem item = new Imageitem ();item.id = source.readstring ();Item.path = source.readstring ();Item.thumbnailpath = source.readstring ();Item.bitmap = Bitmap.CREATOR.createFromParcel (source);return item; }@OverridePublic imageitem[] NewArray (int size) {return new imageitem[size]; } };}
Object serialization of the Android Development series