In Android, how to use intent to transmit objects between activities [using serializable or parcelable]

Source: Internet
Author: User

To transfer objects between different activities in Android, we can consider using bundle. putserializable (Key, object); you can also consider using bundle. putparcelable (Key, object); the object in the previous method must implement the serializable interface, and the object in the next method must implement the parcelable interface. The following is a complete example.

1. Create an android project. The directory structure of the project is as follows:

 

2. Modify the main. xml layout file. The source code of the layout file 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 = "@ string/Hello" <br/> <button <br/> Android: id = "@ + ID/serbutton" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "serializable"/> <br/> <button <br/> Android: Id = "@ + ID/parbutton" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "parcelable"/> <br/> </linearlayout>

3. create an object class package under the src directory of the project and name it COM. andy. entity. at the same time, two entity classes are added to the package. One is person. java, which implements the serializable interface; the other is police. java, which implements the parcelable interface. The Code is as follows:

Person. Java:

Package COM. andy. entity; </P> <p> Import Java. io. serializable; </P> <p> public class person implements serializable {</P> <p> Private Static final long serialversionuid =-6919461967497580316l; </P> <p> private string name; <br/> private int age; </P> <p> 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/>}< br/>}

Police. Java:

Package COM. andy. entity; </P> <p> Import android. OS. parcel; <br/> Import android. OS. parcelable; </P> <p> public class police implements parcelable {</P> <p> private string name; <br/> private int worktime; </P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p> Public int getworktime () {<br/> return worktime; <br/>}</P> <p> Public void setworktime (INT worktime) {<br/> This. worktime = worktime; <br/>}</P> <p> Public static final parcelable. creator <police> creator = new creator <police> () {</P> <p> @ override <br/> Public Police createfromparcel (parcel source) {<br/> police = new police (); <br/> police. name = source. readstring (); <br/> police. worktime = source. readint (); <br/> return police; <br/>}</P> <p> @ override <br/> Public Police [] newarray (INT size) {<br/> return new police [size]; <br/>}< br/> }; </P> <p> @ override <br/> Public int describecontents () {<br/> return 0; <br/>}</P> <p> @ override <br/> Public void writetoparcel (parcel, int flags) {<br/> parcel. writestring (name); <br/> parcel. writeint (worktime); <br/>}< br/>}

4. in the package COM. andy. modify testactivity in testdemo. java class, and added serializabledemo and parcelabledemo classes in the package. They inherit the activity class and display the data of the person object and police object respectively. The Code is as follows:

Package COM. andy. testdemo; </P> <p> Import COM. andy. entity. person; <br/> Import COM. andy. entity. police; </P> <p> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; </P> <p> public class testactivity extends activity {<br/> private button sbutton, pbutton; <br/> Public final static string ser_key = "com. andy. ser "; <br/> Public final static string par_key =" com. andy. par "; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> sbutton = (button) findviewbyid (R. id. serbutton); <br/> sbutton. setonclicklistener (new view. onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> serializemethod (); <br/>}< br/>}); </P> <p> pbutton = (button) findviewbyid (R. id. parbutton); <br/> pbutton. setonclicklistener (new view. onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> pacelablemethod (); <br/>}< br/> }); <br/>}</P> <p>/** <br/> * serializeable Method for passing objects <br/> */<br/> private void serializemethod () {<br/> person mperson = new person (); <br/> mperson. setname ("Andy"); <br/> mperson. setage (26); <br/> intent mintent = new intent (this, serializabledemo. class); <br/> bundle mbundle = new bundle (); <br/> mbundle. putserializable (ser_key, mperson); <br/> mintent. putextras (mbundle); </P> <p> startactivity (mintent ); <br/>}</P> <p>/** <br/> * pacelable object transfer method <br/> */<br/> private void pacelablemethod () {<br/> police mpolice = new police (); <br/> mpolice. setname ("I am police"); <br/> mpolice. setworktime (2008); <br/> intent mintent = new intent (this, parcelabledemo. class); <br/> bundle mbundle = new bundle (); <br/> mbundle. putparcelable (par_key, mpolice); <br/> mintent. putextras (mbundle); </P> <p> startactivity (mintent); <br/>}< br/>}

Serializabledemo. Java class

Package COM. andy. testdemo; </P> <p> Import COM. andy. entity. person; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; </P> <p> public class serializabledemo extends activity {</P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> textview mtextview = new textview (this); <br/> person mperson = (person) getintent (). getserializableextra (testactivity. ser_key); <br/> mtextview. settext ("You name is:" + mperson. getname () + "/N" + <br/> "you age is:" + mperson. getage (); </P> <p> setcontentview (mtextview); <br/>}</P> <p>}

Parcelabledemo. Java class:

Package COM. andy. testdemo; </P> <p> Import COM. andy. entity. police; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; </P> <p> public class parcelabledemo extends activity {</P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> textview mtextview = new textview (this); <br/> police mpolice = (police) getintent (). getparcelableextra (testactivity. par_key); <br/> mtextview. settext ("police name is:" + mpolice. getname () + "/N" + <br/> "worktime is:" + mpolice. getworktime () + "/N"); <br/> setcontentview (mtextview); <br/>}</P> <p>}

5. register the newly added two activities in the androidmanifest. xml file.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. andy. testdemo "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". testactivity "<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 = ". serializabledemo "/> <br/> <activity Android: Name = ". parcelabledemo "/> <br/> </Application> <br/> <uses-SDK Android: minsdkversion =" 8 "/> </P> <p> </manifest>

6. Run the program to view:

[1] main interface:

[2] Click the serializable button.

[3] Effect of clicking the parcelable button

 

========================================================== ======================================

The preceding example shows how to use intent to transfer objects between different activities.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.