Android passes objects and arrays of objects between multiple activity

Source: Internet
Author: User

Assume that the object is a people class that contains information about the name and age:

public class People{public String Strname;public int iage;public people (String strname,int iage) {this.strname = Strname;t His.iage = iage;} Public String GetName () {return strName;} public int getage () {return iage;}}

Method One: Serializable

Required condition: class implements the Serializable interface

Public class people implements serializable{private static final long serialversionuid = 1l;public String strname;public i NT Iage;public people (String strname,int iage) {this.strname = Strname;this.iage = iage;} Public String GetName () {return strName;} public int getage () {return iage;}}

Passing objects:

Delivery End:

People people = New People ("John"), Intent Intent = new Intent (sendactivity.this,rcvactivity.class); Bundle bundle = new bundle (); Bundle.putserializable ("People", people); Intent.putextras (bundle); StartActivity (Intent );

Receiving end:

People people = (people) this.getintent (). Getserializableextra ("people"); String strdata = people.getname () + "" + people.getage (); Toast.maketext (Getapplication (), Strdata, Toast.length_short). Show ();

To pass an array of objects:

Delivery End:

list<people> people = new arraylist<people> ();p Eople.add (New People ("John"));p Eople.add (New People (" Amy ", 20)); Bundle bundle = new bundle (); Bundle.putserializable ("People", (Serializable) people); Intent Intent = new Intent ( Sendactivity.this, Rcvactivity.class); Intent.putextras (bundle); startactivity (intent);

Receiving end:

List<people> resultlist = (list<people>) this.getintent (). Getserializableextra ("People"); String strdata = ""; for (people p:resultlist) {strdata = strdata + P.strname + "" + p.iage + "\ n";} Toast.maketext (Getapplication (), Strdata, Toast.length_short). Show ();


Method Two: Parcelable

Required condition: class implements the Parcelable interface

Public class people implements parcelable {public String strname;public int iage;public people (String strname,int iage) {th Is.strname = Strname;this.iage = iage;} Public String GetName () {return strName;} public int getage () {return iage;} @Overridepublic int describecontents () {//TODO auto-generated method Stubreturn 0;} @Overridepublic void Writetoparcel (Parcel Parcel, int arg1) {//TODO auto-generated method Stubparcel.writestring ( StrName);p arcel.writeint (iage);} public static final parcelable.creator<people> Creator = new creator<people> () {public people Createfromparcel (Parcel source) {People ptemp = new People ("", 0);p temp.strname = source.readstring ();p temp.iage = Source . ReadInt (); return ptemp;} Public people[] NewArray (int size) {return new people[size];};}

Passing objects:

Delivery End:
People people = New People ("John"), Intent Intent = new Intent (sendactivity.this,rcvactivity.class); Bundle bundle = new bundle (); Bundle.putparcelable ("People", people); Intent.putextras (bundle); startactivity (intent);

Receiving end:

People people = (people) this.getintent (). Getparcelableextra ("people"); String strdata = people.getname () + "" + people.getage (); Toast.maketext (Getapplication (), Strdata, Toast.length_short). Show ();

To pass an array of objects:

Delivery End:

list<people> people = new arraylist<people> (); People.add (New People ("John", 21)); People.add (New People ("Amy"), Intent Intent = new Intent (sendactivity.this,rcvactivity.class); Bundle bundle = new bundle (); Bundle.putparcelablearraylist ("People", (arraylist< extends parcelable>) people); Intent.putextras (bundle); startactivity (intent);

Receiving end:

list<people> resultlist = This.getintent (). Getextras (). Getparcelablearraylist ("people"); String strdata = ""; for (people p:resultlist) {strdata = strdata + P.strname + "" + p.iage + "\ n";} Toast.maketext (Getapplication (), Strdata, Toast.length_short). Show ();

You can find that you need to implement public int describecontents (), Publicvoid writetoparcel (Parcel Parcel, int arg1) in parcelable, and also add a static member variable CRE Ator:public static final parcelable.creator<people> Creator.

Difference (by:http://www.cnblogs.com/renqingping/archive/2012/10/25/parcelable.html)

1.Serializable implementation, only need implements serializable can. This simply marks the object and the system automatically serializes it.

The implementation of 2.Parcelabel requires not only implements Parcelabel, but also the addition of a static member variable Creator to the class, which requires the implementation of the Parcelable.creator interface.

3. When using memory, parcelable analogy serializable high performance, so we recommend the use of Parcelable class. 4.Serializable generates a large number of temporary variables when serializing, causing frequent GC.

5.Parcelable cannot be used in situations where data is stored on disk because Parcelable does not guarantee the continuity of data in the event of a change in the outside world.







Android passes objects and arrays of objects between multiple activity

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.