標籤:activity android serializable parcelable arraylist
假設對象為People類,包含資訊姓名和年齡:
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;}}
方法一:Serializable
必須條件:類實現了Serializable介面
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;}}
傳遞對象:
傳遞端:
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);
接收端:
People people = (People) this.getIntent().getSerializableExtra("people");String strData = people.getName() + " " + people.getAge();Toast.makeText(getApplication(),strData, Toast.LENGTH_SHORT).show();
傳遞對象數組:
傳遞端:
List<People> people = new ArrayList<People>();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);
接收端:
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();
方法二:Parcelable
必須條件:類實現了Parcelable介面
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<People> CREATOR = new Creator<People>() {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];}};}
傳遞對象:
傳遞端:
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);
接收端:
People people = (People) this.getIntent().getParcelableExtra("people");String strData = people.getName() + " " + people.getAge();Toast.makeText(getApplication(),strData, Toast.LENGTH_SHORT).show();
傳遞對象數組:
傳遞端:
List<People> People = new ArrayList<People>();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<? extends Parcelable>) People);intent.putExtras(bundle);startActivity(intent);
接收端:
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();
可以發現在Parcelable中需實現public int describeContents()、 publicvoid writeToParcel(Parcel parcel, int arg1),還需要在添加一個靜態成員變數CREATOR:public static final Parcelable.Creator<People> CREATOR。
區別(by: http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html)
1.Serializable的實現,只需要implements Serializable即可。這隻是給對象打了一個標記,系統會自動將其序列化。
2.Parcelabel的實現,不僅需要implements Parcelabel,還需要在類中添加一個靜態成員變數CREATOR,這個變數需要實現 Parcelable.Creator 介面。
3.在使用記憶體的時候,Parcelable 類比Serializable效能高,所以推薦使用Parcelable類。4.Serializable在序列化的時候會產生大量的臨時變數,從而引起頻繁的GC。
5.Parcelable不能使用在要將資料存放區在磁碟上的情況,因為在外界有變化的情況下Parcelable不能很好的保證資料的持久性。
Android在多個Activity間傳遞對象及對象數組