To pass an object between two activity, then this object must be serialized, there are two ways to serialize an object in Android, one is to implement the Serializable interface, this is very simple, just need to declare it can be, perfunctory. But there is also a unique serialization method in Android, which is to implement the Parcelable interface, which is more efficient than implementing the serializable interface in this way. However, the serializable interface is very convenient, so in some cases the implementation of this interface is a very good choice.
To use the parcelable step:
1. Implement the Parcelable interface
2. Implementing two methods in an interface
publicintdescribeContents();publicvoidwriteToParcelint flags);
The first method is the content interface description, which is returned by default to 0.
The second method is to serialize our object to a parcel object, that is, to store our objects in parcel
3. Instantiate the static internal object Creator implement the interface Parcelable.creator, instantiate the Creator to implement the two methods, wherein createFromParcel the function is to read our object from parcel.
In other words, we first use methods to write to the writeToParcel object, and then use the createFromParcel method to read the object, so the reading and writing order in the two methods must be consistent, otherwise there will be data disturbances, I will give an example.
Look at a code example:
Public class person implements parcelable{ PrivateString username;PrivateString nickname;Private intAge PublicStringGetUserName() {returnUsername } Public void Setusername(String username) { This. Username = Username; } PublicStringGetnickname() {returnNickname; } Public void Setnickname(String nickname) { This. nickname = nickname; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } Public Person(string username, string nickname,intAge) {Super(); This. Username = Username; This. nickname = nickname; This. Age = Age; } Public Person() {Super(); }/** * Here the order must be read in accordance with Writetoparcel (Parcel dest, int flags) method * Write in the same order, otherwise the data will be wrong, such as your reading order if: * nickname = Sourc E.readstring (); * Username=source.readstring (); * age = Source.readint (); * That is, swap the username and nickname reading order, then you will find that you get the username is nickname data, * and you get nickname is username data * @param Source */ Public Person(Parcel Source) {username = source.readstring (); Nickname=source.readstring (); Age = Source.readint (); }/** * Return 0 here by default * / @Override Public int describecontents() {return 0; }/** * Write the value into parcel * / @Override Public void Writetoparcel(Parcel dest,intFlags) {dest.writestring (username); Dest.writestring (nickname); Dest.writeint (age); } Public Static FinalCreator<person> Creator =NewCreator<person> () {/** * For the outer class to deserialize this class array using */ @Override PublicPerson[]NewArray(intSize) {return NewPerson[size]; }/** * reading data from parcel * / @Override PublicPersonCreatefromparcel(Parcel Source) {return Newperson (source); } };}
The source of the project Http://pan.baidu.com/s/1hqzY3go
Finally affixed to the parcelable source code, Google has given an example:
/* Copyright (C) 2006 the Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "Licen Se "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageAndroid.os;/** * Interface for classes whose instances can is written to * and restored from a {@linkParcel}. Classes implementing the Parcelable * interface must also have a static field called <code>creator</code>, whi CH * is an object implementing the {@linkParcelable.creator parcelable.creator} * interface. * * <p>a Typical implementation of parcelable is:</p> * * <pre> * public class Myparcelable implements parcelable {* private int mdata; * public int describecontents () {* return 0; *} * * Public void Writetoparcel (Parcel out, int flags) {* Out.writeint (mdata); *} * public static final parcelable. creator< Myparcelable> CREATOR * = new parcelable.creator< Myparcelable> () {* Public myparcelable createfromparcel (Parcel in) {* Return new myparcelable (in); *} * Public myparcelable[] NewArray (int size) {* return new myparcelable[size]; * } * }; * Private myparcelable (Parcel in) {* Mdata = In.readint (); *} *}</pre> * *Public interface Parcelable {/** * Flag for use with {@link #writeToParcel}: The object being written * is a return value, which is The result of a function such as * "<code>parcelable someFunction () </code>", * "<code>void some Function (out parcelable) </code> ", or *" <code>void someFunction (inout parcelable) </code> ". Some implementations * may want to release resources at this point. */public staticFinalint parcelable_write_return_value =0x0001;/** * Bit masks for use with {@link #describeContents}: Each Bit represents a * kind of object consid Ered to has potential special significance when * marshalled. */public staticFinalint contents_file_descriptor =0x0001;/** * Describe The kinds of special objects contained in this parcelable ' s * marshalled representation. * * @return A bitmask indicating the set of special object types marshalled * by the parcelable. */public int describecontents ();/** * Flatten This object with to a Parcel. * * @param dest The Parcel in which the object should is written. * @param The flags Additional flags about how the object should is written. * May is 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}. */public void Writetoparcel (Parcel dest, int flags);/** * Interface that must is implemented and provided as a public CREATOR * field that generates instances of Your parcelable class from a Parcel. */Public interface Creator<t> {/** * Create A new instance of the Parcelable class, instantiating it * from the given Parcel whose da Ta had previously been written by * {@link parcelable#writetoparcel parcelable.writetoparcel ()}. * * @param SOURCE The Parcel to read the object's data from. * @return Returns A new instance of the Parcelable class. */Public T Createfromparcel (Parcel source);/** * Create A new array of the Parcelable class. * * @param size size of the array. * @return Returns An array of the Parcelable class, with every entry * initialized to NULL. */Public t[] NewArray (int size); }/** * Specialization of {@link Creator} that allows-to-receive the * ClassLoader the object is being ing created in. */public interface classloadercreator<t>extendscreator<t> {/** * Create A new instance of the Parcelable class, instantiating it * from the given Parcel whose da Ta had previously been written by * {@link parcelable#writetoparcel parcelable.writetoparcel ()} and * Using the given ClassLoader. * * @param SOURCE The Parcel to read the object's data from. * @param Loader The ClassLoader, this object was being created in. * @return Returns A new instance of the Parcelable class. */Public T Createfromparcel (Parcel source, ClassLoader loader); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.
Android Development Parcelable use of the detailed