Hello everyone, I haven't seen you for a long time. Today I want to tell you how to pass objects in intent in Android. As far as I know, there are two methods: bundle. putserializable (Key, object); bundle. putparcelable (Key, object); of course, these objects have certain conditions. The former implements the serializable interface, and the latter implements the parcelable interface, to make it easier for everyone to understand, I wrote a simple demo as usual. Let's come with me step by step!
Step 1: Create an android project named objecttrandemo (many classes !) The directory structure is as follows:
Step 2: Modify the main. xml layout file (here I have added two buttons). The Code is as follows:<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "welcome to Mr Wei's blog. "<br/> <button <br/> Android: Id =" @ + ID/button1 "<br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "serializable" <br/> <button <br/> Android: Id = "@ + ID/button2" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "parcelable" <br/> </linearlayout>
Step 3: create two classes: one is the person. Java serializable interface, and the other book. Java implements the parcelable interface. The Code is as follows:
Person. Java:
Package COM. tutor. objecttran; <br/> Import Java. io. serializable; <br/> public class person implements serializable {<br/> Private Static final long serialversionuid =-7060210544600464481l; <br/> private string name; <br/> private int age; <br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public int getage () {<br/> return age; <br/>}< br/> Public void setage (INT age) {<br/> This. age = age; <br/>}</P> <p >}< br/>
Book. Java:
Package COM. tutor. objecttran; <br/> Import android. OS. parcel; <br/> Import android. OS. parcelable; <br/> public class Book implements parcelable {<br/> private string bookname; <br/> private string author; <br/> private int publishtime; </P> <p> Public String getbookname () {<br/> return bookname; <br/>}< br/> Public void setbookname (string bookname) {<br/> This. bookname = bookname; <br/>}< br/> Public String getauthor () {<br/> return author; <br/>}< br/> Public void setauthor (string author) {<br/> This. author = author; <br/>}< br/> Public int getpublishtime () {<br/> return publishtime; <br/>}< br/> Public void setpublishtime (INT publishtime) {<br/> This. publishtime = publishtime; <br/>}</P> <p> Public static final parcelable. creator <book> creator = new creator <book> () {<br/> Public book createfromparcel (parcel source) {<br/> book mbook = New Book (); <br/> mbook. bookname = source. readstring (); <br/> mbook. author = source. readstring (); <br/> mbook. publishtime = source. readint (); <br/> return mbook; <br/>}< br/> Public book [] newarray (INT size) {<br/> return New Book [size]; <br/>}< br/>}; </P> <p> Public int describecontents () {<br/> return 0; <br/>}< br/> Public void writetoparcel (parcel, int flags) {<br/> parcel. writestring (bookname); <br/> parcel. writestring (author); <br/> parcel. writeint (publishtime); <br/>}< br/>
Step 4: Modify objecttrandemo. Java and create two new activities, objecttrandemo1.java and objecttrandemo2.java, to display the person object data and book object data respectively. The Code is as follows:
Objecttrandemo. Java:
Package COM. tutor. objecttran; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> public class objecttrandemo extends activity implements onclicklistener {</P> <p> private button sbutton, pbutton; <br/> Public final static string ser_key = "com. tutor. objecttran. ser "; <br/> Public final static string par_key =" com. tutor. objecttran. par "; <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> setupviews (); </P> <p >}</P> <p> // my consistent style <br/> Public void setupviews () {<br/> sbutton = (button) findviewbyid (R. id. button1); <br/> pbutton = (button) findviewbyid (R. id. button2); <br/> sbutton. setonclicklistener (this); <br/> pbutton. setonclicklistener (this); <br/>}< br/> // serializeable Method for passing objects <br/> Public void serializemethod () {<br/> person mperson = new person (); <br/> mperson. setname ("Frankie"); <br/> mperson. setage (25); <br/> intent mintent = new intent (this, objecttrandemo1.class); <br/> bundle mbundle = new bundle (); <br/> mbundle. putserializable (ser_key, mperson); <br/> mintent. putextras (mbundle); </P> <p> startactivity (mintent ); <br/>}< br/> // pacelable object transfer method <br/> Public void pacelablemethod () {<br/> book mbook = New Book (); <br/> mbook. setbookname ("android tutor"); <br/> mbook. setauthor ("Frankie"); <br/> mbook. setpublishtime (2010); <br/> intent mintent = new intent (this, objecttrandemo2.class); <br/> bundle mbundle = new bundle (); <br/> mbundle. putparcelable (par_key, mbook); <br/> mintent. putextras (mbundle); </P> <p> startactivity (mintent ); <br/>}< br/> // response to the event when the button is clicked <br/> Public void onclick (view v) {<br/> If (V = sbutton) {<br/> serializemethod (); <br/>}else {<br/> pacelablemethod (); <br/>}< br/>}
Objecttrandemo1.java:
Package COM. tutor. objecttran; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; <br/> public class objecttrandemo1 extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> textview mtextview = new textview (this); <br/> person mperson = (person) getintent (). getserializableextra (objecttrandemo. ser_key); <br/> mtextview. settext ("You name is:" + mperson. getname () + "/N" + <br/> "you age is:" + mperson. getage (); </P> <p> setcontentview (mtextview); <br/>}< br/>}
Objecttrandemo2.java:
Package COM. tutor. objecttran; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; <br/> public class objecttrandemo2 extends activity {</P> <p> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> textview mtextview = new textview (this); <br/> book mbook = (book) getintent (). getparcelableextra (objecttrandemo. par_key); <br/> mtextview. settext ("book name is:" + mbook. getbookname () + "/N" + <br/> "author is:" + mbook. getauthor () + "/N" + <br/> "publishtime is:" + mbook. getpublishtime (); <br/> setcontentview (mtextview); <br/>}< br/>}
Step 5: Next, modify the androidmanifest. xml file (declare the code of the two newly added activities, objecttrandemo1 and objecttrandemo2) as follows (lines 14th and 15 ):
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. tutor. objecttran "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". objecttrandemo "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <activity Android: Name = ". objecttrandemo1 "> </activity> <br/> <activity Android: Name = ". objecttrandemo2 "> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion =" 7 "/> <br/> </manifest>
Step 6: run the above project to view it:
Effect 1: First interface:
Result 2: Click serializable.
Effect 3: Click parcelable:
OK ~ Today, we will come here first. If you want to think the code is too long and you don't want to repeat it, you can leave an email for you ~ I hope it will be helpful to you, Bye ~