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.