First of all, we need to know that there are two methods of Android serialization, one is to implement serializable, which is the interface supported in Javase, Google later launched an Android-specific interface parcelable (which has a performance serializable nearly 10 times times faster)
And then we need to know the basic way to transfer instances inside Android: one is bundle.putserializable (Key,object), the other is bundle.putparcelable (Key,object), They are implemented serializable and parcelable interfaces, respectively. One thing to keep in mind is that parcelable cannot be used in situations where data is stored on disk because Parcelable does not guarantee the continuity of the data in the event of a change in the outside world. It's better to use serializable in this case.
Let's look at the definition of the Parcelable interface:
1 Public Interfaceparcelable2 {3 //content Description interface, basic no tube4 Public intdescribecontents ();5 //Write interface functions, packaging6 Public voidWritetoparcel (Parcel dest,intflags);7 //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 to inherit the class name passed through the template parameter8 //To enable the introduction of template parameters, the creator embedded interface is defined here, containing two interface functions to return individual and multiple instances of inheriting classes, respectively9 Public InterfaceCreator<t>Ten { One PublicT Createfromparcel (Parcel source); A PublicT[] NewArray (intsize); - } - Public InterfaceClassloadercreator<t>extendsCreator<t> the { - PublicT Createfromparcel (Parcel source, ClassLoader loader); - } -}
After inheriting the interface, we mainly rewrite Writetoparcel () to serialize the data into a parcel object
Here is a user class to implement the parcelable of the specific source code
1 PackageCom.catvideo.tv.catvideo.com.catvideo.user;2 3 ImportAndroid.os.Parcel;4 Importandroid.os.Parcelable;5 6 /**7 * @authorLCC [email protected]8 * @version2016?? 5?? 31?? ???? 12:09:209 * @DescriptionTen */ One Public classUserImplementsparcelable { A intuserId; - PrivateString UserName; - PrivateString PassWord; the PrivateString Email; - BooleanState ; - - PublicUser () { + - } + A PublicUser (string email, string passWord) { at This. email =email; - This. PassWord =PassWord; - } - - PublicUser (intUserId, String userName, String PassWord,BooleanState ) { - This. UserId =userId; in This. UserName =UserName; - This. PassWord =PassWord; to This. State =State ; + } - the PublicUser (intuserId, String userName, String PassWord) { * This. UserId =userId; $ This. UserName =UserName;Panax Notoginseng This. PassWord =PassWord; - This. State =false; the } + A Public intgetUserId () { the returnuserId; + } - $ Public voidSetuserid (intuserId) { $ This. UserId =userId; - } - the PublicString GetUserName () { - returnUserName;Wuyi } the - Public voidsetusername (String userName) { Wu This. UserName =UserName; - } About $ PublicString GetPassword () { - returnPassWord; - } - A Public voidSetPassword (String passWord) { + This. PassWord =PassWord; the } - $ the PublicString Getemail () { the returnemail; the } the - Public voidsetemail (String email) { in This. email =email; the } the About Public Booleanisstate () { the returnState ; the } the + Public voidSetState (BooleanState ) { - This. State =State ; the }Bayi the @Override the Public intdescribecontents () { - return0; - } the the @Override the Public voidWritetoparcel (Parcel dest,intflags) { the Boolean[] states ={state}; - dest.writestring (userName); the Dest.writeint (GetUserId ()); the Dest.writebooleanarray (states); the }94 the //Public static final parcelable.creator<user> Creator = new parcelable.creator<user> () { the ////Rewrite creator the //98 //@Override About //Public User Createfromparcel (Parcel source) { - //User user = new user ();101 ////user.username=source.readstring ()102 //103 ////P.map = Source.readhashmap (HashMap.class.getClassLoader ());104 ////p.name = source.readstring (); the //return p;106 // }107 //108 //@Override109 //Public user[] NewArray (int size) { the // //TODO auto-generated Method Stub111 //return null; the // }113 // }; the}
You can then use this:
Intent.putextra ("user", user);
or use Bundle.putparcelable (key,object)
Serializable implementation and Parcelabel the difference between implementations
1) Serializable implementation, only need implements Serializable can, do not need to implement any method. This simply marks the object and the system automatically serializes it.
2) Parcelabel implementation, not only need to implements Parcelabel, but also to add a static member variable Creator in the class, this variable needs to implement the Parcelable.creator interface.
Use/serialization data for Android Parcelable interface