Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android在多個Activity間傳遞對象及對象數組

Android在多個Activity間傳遞對象及對象數組

編輯:關於Android編程

假設對象為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 = 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);

接收端:

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


方法二: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;
	}
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public void writeToParcel(Parcel parcel, int arg1) {
		// TODO Auto-generated method stub
		parcel.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];
		}
	};
}

傳遞對象:

傳遞端:
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 = 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);

接收端:

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

可以發現在Parcelable中需實現public int describeContents()、 publicvoid writeToParcel(Parcel parcel, int arg1),還需要在添加一個靜態成員變量CREATOR:public static final Parcelable.Creator 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不能很好的保證數據的持續性。







  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved