Note: This article is adapted from the Android_tutor article, the original address: http://blog.csdn.net/android_tutor/article/details/5740845
There are two ways to pass in Android, one is serializable and the other is parcelable.
Serializable is supported by J2SE itself. And Parcelable is unique to Android.
The use scenarios and differences between the two:
1) When using memory, parcelable performance is higher than serializable, so we recommend using Parcelable.
2) serializable generates a large number of temporary variables at serialization time, resulting in frequent GC.
3) parcelable can not be used in the case of storing data on disk, because Parcelable does not guarantee the continuity of the data in the event of external change. Although serializable is inefficient, it is recommended to use serializable at this point.
Serializable interface implementation is relatively simple, as long as the implementation setter and getter, it is possible.
And the parcelable implementation is more troublesome. Use a small demo to illustrate:
1. New Android Project Objecttrandemo
2. Create an entity JavaBean:Person.java. Implementing the Serializable interface
Package Com.example.objecttrandemo;import Java.io.serializable;public class Person implements Serializable { Private static final long serialversionuid = -7060210544600464481l; private String name; private int age; Public String GetName () { return name; } public void SetName (String name) { this.name = name; } public int getage () { return age; } public void Setage (int.) { this.age = age; } }
3. Create the main interface activity_main.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " ><textview android:layout_width=" fill_parent " android:layout_height=" Wrap_ Content " android:text=" Android object Trance " /><buttonandroid:id=" @+id/button1 "Android:layout_ Width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Serializable"/><buttonandroid:id= "@ +id/button2 "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:text=" Parcelable "/ ></LinearLayout>
4. Create the main activity. This activity is primarily used to respond to click events and to pass data. Objecttrandemo.java:
Package Com.example.objecttrandemo;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; public class Objecttrandemo extends Activity implements Onclicklistener{private Button Sbutton,pbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Setupviews (); }//initial the public void Setupviews () {Sbutton = (Button) Findviewbyid (R.id.button1); Pbutton = (Button) Findviewbyid (R.id.button2); Sbutton.setonclicklistener (this); Pbutton.setonclicklistener (this); }//serializeable Object Trance public void Serializemethod () {Person Mperson = new person (); Mperson.setname ("Durant"); Mperson.setage (25); Intent mintent = new Intent (this,objecttrandemo1.class); Bundle Mbundle = new bundle (); Mbundle.putserialIzable ("Mperson", Mperson); Mintent.putextras (Mbundle); StartActivity (mintent); }//pacelable Object Trance public void Pacelablemethod () {Book Mbook = new book (); Mbook.setbookname ("A Man From Mars"); Mbook.setauthor ("James"); Mbook.setpublishtime (2014); Intent mintent = new Intent (this,objecttrandemo2.class); Bundle Mbundle = new bundle (); Mbundle.putparcelable ("Mbook", Mbook); Mintent.putextras (Mbundle); StartActivity (mintent); } @Overridepublic void OnClick (View v) {if (v = = Sbutton) {Serializemethod (); }else{Pacelablemethod (); }} }
5. New activity Objecttrandemo1.java to display the data passed by the Serializable interface
Package Com.example.objecttrandemo;import Android.app.activity;import Android.os.bundle;import Android.widget.textview;public class ObjectTranDemo1 extends activity{@Override public void OnCreate (Bundle Savedinstancestate) { super.oncreate (savedinstancestate); TextView Mtextview = new TextView (this); Person Mperson = (person) getintent (). Getserializableextra ("Mperson"); Mtextview.settext ("You name is:" + mperson.getname () + "\ n" + "You are :" + mperson.getage ()); Setcontentview (Mtextview); } }
At this point, the serializable interface passes the data, yes, and don't forget to declare the new activity in the Mainfest.xml file.
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.objecttrandemo "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses- SDK android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbac" Kup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@styl E/apptheme "> <activity android:name=" Com.example.objecttrandemo.ObjectTranDemo "Androi D:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.ac tion. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filte r> </activity> <activity android:name= ". ObjectTranDemo1 "></activity> <activity android:name=". ObjectTranDemo2"></activity> </application></manifest>
Here's a look at Parcelable's delivery:
The implementation of Parcelable generally requires 4 steps:
1) Implements Parcelable
2) Rewrite the Writetoparcel method to serialize your object to a parcel object, that is, to write the data of the class to an externally supplied parcel, to package the data that needs to be passed to the parcel container to save the data from the parcel container
3) Rewrite the Describecontents method, the Content interface description, the default return 0 can be
4) Instantiate the static internal object Creator Implement Interface Parcelable.creator
public static final Parcelable.creator<t> Creator
Note: The public static final one can not be less, the internal object creator name can not be changed, must all uppercase. The two methods in this interface need to be rewritten: the Createfromparcel (Parcel in) Implementation reads the passed data value from the Parcel container, encapsulates the Parcelable object to return the logical layer, and newarray (int size) creates a type of T, An array of size length, with just one sentence (return new t[size]), for the outer class to deserialize the array of this class.
In short: Map your objects into parcel objects through writetoparcel, and then map parcel objects to your objects through Createfromparcel. It is also possible to think of parcel as a stream through which the object is written to the stream through the Writetoparcel, and the object is read from the stream through the Createfromparcel, but this process needs to be done, so the order of writing and the order of reading must be consistent.
Specific:
1. Create a new book entity class:
Book.java:
Package Com.example.objecttrandemo;import Android.os.parcel;import Android.os.parcelable;public class book Implements Parcelable{private String BookName; Private String author; private int publishtime;public String getbookname () {return bookname;} public void Setbookname (String bookname) {this.bookname = BookName;} Public String Getauthor () {return author;} public void Setauthor (String author) {this.author = author;} public int Getpublishtime () {return publishtime;} public void setpublishtime (int publishtime) {this.publishtime = Publishtime;} Internal Description interface,you do not need to manage@overridepublic int describecontents () {return 0;}//give some A Ttention to the Oder Betwwen Writetoparcel and createfromparcel@overridepublic void Writetoparcel (Parcel Parcel, int flag s) {parcel.writestring (bookname);p arcel.writestring (author);p Arcel.writeint (publishtime);} public static final parcelable.creator<book> Creator = new creator<book> () {@Overridepublic book[] NewArray (int size) {return new book[size];} @Overridepublic book Createfromparcel (Parcel source) {Book Mbook = new book (); Mbook.bookname = Source.readstring (); Mbook.author = Source.readstring (); Mbook.publishtime = Source.readint (); return mbook;}; }2. Create a new Activity:ObjectTranDemo2.java. Data to display parcelable:
Package Com.example.objecttrandemo;import Android.app.activity;import Android.os.bundle;import Android.widget.textview;public class ObjectTranDemo2 extends activity{public void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); TextView Mtextview = new TextView (this); Book Mbook = (book) getintent (). Getparcelableextra ("Mbook"); Mtextview.settext ("Book Name is:" + mbook.getbookname () + "\ n" + "Author is:" + mbook.getauthor () + "\ n" + " Publish Time is: "+ mbook.getpublishtime ()); Setcontentview (Mtextview); } }
Android intent two ways to pass objects in Android serializable,parcelable