Android transmits objects and object arrays between multiple activities

Source: Internet
Author: User

Android transmits objects and object arrays between multiple activities

Assume that the object is in the People class, including the information name and age:

public class People{public String strName;public int iAge;public People(String strName,int iAge){this.strName = strName;this.iAge = iAge;}public String getName(){return strName;}public int getAge(){return iAge;}}

Method 1: Serializable

Required condition: the class implements the Serializable interface.

public class People implements Serializable{private static final long serialVersionUID = 1L;public String strName;public int iAge;public People(String strName,int iAge){this.strName = strName;this.iAge = iAge;}public String getName(){return strName;}public int getAge(){return iAge;}}

Transfer object:

Transfer end:

People people = new People("John", 21);Intent intent = new Intent(SendActivity.this,RcvActivity.class);Bundle bundle = new Bundle();bundle.putSerializable("people", people);intent.putExtras(bundle);startActivity(intent);

Acceptor:

People people = (People) this.getIntent().getSerializableExtra("people");String strData = people.getName() + " " + people.getAge();Toast.makeText(getApplication(),strData, Toast.LENGTH_SHORT).show();

Passing object array:

Transfer end:

List
 
   people = new ArrayList
  
   ();people.add(new People("John", 21));people.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);
  
 

Acceptor:

List
 
   resultList = (List
  
   ) 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 2: Parcelable

Required condition: the class implements the Parcelable interface.

public class People implements Parcelable {public String strName;public int iAge;public People(String strName,int iAge){this.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);parcel.writeInt(iAge);}public static final Parcelable.Creator
 
   CREATOR = new Creator
  
   () {public People createFromParcel(Parcel source) {People pTemp = new People("",0);pTemp.strName = source.readString();pTemp.iAge = source.readInt();return pTemp;}public People[] newArray(int size) {return new People[size];}};}
  
 

Transfer object:

Transfer end:
People people = new People("John", 21);Intent intent = new Intent(SendActivity.this,RcvActivity.class);Bundle bundle = new Bundle();bundle.putParcelable("people", people);intent.putExtras(bundle);startActivity(intent);

Acceptor:

People people = (People) this.getIntent().getParcelableExtra("people");String strData = people.getName() + " " + people.getAge();Toast.makeText(getApplication(),strData, Toast.LENGTH_SHORT).show();

Passing object array:

Transfer end:

List
 
   People = new ArrayList
  
   ();People.add(new People("John", 21));People.add(new People("Amy", 20));Intent intent = new Intent(SendActivity.this,RcvActivity.class);Bundle bundle = new Bundle();bundle.putParcelableArrayList("People",(ArrayList
   ) People);intent.putExtras(bundle);startActivity(intent);
  
 

Acceptor:

List
 
   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();
 

It can be found that in Parcelable, public int describeContents () and publicvoid writeToParcel (Parcel parcel, int arg1) must be implemented, and a static member variable CREATOR: public static final Parcelable. Creator must be added. CREATOR.

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

1. The implementation of Serializable only requires implements Serializable. This only marks the object and the system will automatically serialize it.

2. The implementation of Parcelabel requires not only implements Parcelabel, but also a static member variable CREATOR added to the class. This variable must implement the Parcelable. Creator interface.

3. Parcelable is superior to Serializable in memory usage, so Parcelable class is recommended. 4. Serializable will generate a large number of temporary variables during serialization, resulting in frequent GC.

5. Parcelable cannot be used to store data on disks, because Parcelable cannot guarantee data continuity in the case of external changes.







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.