Android Activity之間傳遞對象及對象數(shù)組

假設對象為People類,包含信息姓名和年齡:?

publicclass People{?

publicString strName;?

publicint iAge;?

publicPeople(String strName,intiAge){?

this.strName = strName;?

this.iAge = iAge;?

}?

publicString getName(){?

returnstrName;?

}?

publicint getAge(){?

returniAge;?

}?

}?

方法一:Serializable?

必須條件:類實現(xiàn)了Serializable接口?

publicclass People implementsSerializable{?

privatestatic final long serialVersionUID = 1L;?

publicString strName;?

publicint iAge;?

publicPeople(String strName,intiAge){?

this.strName = strName;?

this.iAge = iAge;?

}?

publicString getName(){?

returnstrName;?

}?

publicint getAge(){?

returniAge;?

}?

}?

傳遞對象:?

傳遞端:?

People people = newPeople("John",21);?

Intent intent = newIntent(SendActivity.this,RcvActivity.class);?

Bundle bundle = newBundle();?

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

傳遞對象數(shù)組:?

傳遞端:?

List people = newArrayList();?

people.add(newPeople("John",21));?

people.add(newPeople("Amy",20));?

Bundle bundle = newBundle();?

bundle.putSerializable("people", (Serializable) people);?

Intent intent = newIntent(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?

必須條件:類實現(xiàn)了Parcelable接口?

publicclass People implementsParcelable {?

publicString strName;?

publicint iAge;?

publicPeople(String strName,intiAge){?

this.strName = strName;?

this.iAge = iAge;?

}?

publicString getName(){?

returnstrName;?

}?

publicint getAge(){?

returniAge;?

}?

@Override?

publicint describeContents() {?

// TODO Auto-generated method stub?

return0;?

}?

@Override?

publicvoid writeToParcel(Parcel parcel, intarg1) {?

// TODO Auto-generated method stub?

parcel.writeString(strName);?

parcel.writeInt(iAge);?

}?

publicstatic final Parcelable.Creator CREATOR = newCreator() {?

publicPeople createFromParcel(Parcel source) {?

People pTemp = newPeople("",0);?

pTemp.strName = source.readString();?

pTemp.iAge = source.readInt();?

returnpTemp;?

}?

publicPeople[] newArray(intsize) {?

returnnew People[size];?

}?

};?

}?

傳遞對象:?

傳遞端:?

People people = newPeople("John",21);?

Intent intent = newIntent(SendActivity.this,RcvActivity.class);?

Bundle bundle = newBundle();?

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

傳遞對象數(shù)組:?

傳遞端:?

List People = newArrayList();?

People.add(newPeople("John",21));?

People.add(newPeople("Amy",20));?

Intent intent = newIntent(SendActivity.this,RcvActivity.class);?

Bundle bundle = newBundle();?

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


可以發(fā)現(xiàn)在Parcelable中需實現(xiàn)public int describeContents()、 publicvoid writeToParcel(Parcel parcel, int arg1),還需要在添加一個靜態(tài)成員變量CREATOR:public static final Parcelable.Creator CREATOR。?

區(qū)別:

1.Serializable的實現(xiàn),只需要implements Serializable即可。這只是給對象打了一個標記,系統(tǒng)會自動將其序列化。?

2.Parcelabel的實現(xiàn),不僅需要implements Parcelabel,還需要在類中添加一個靜態(tài)成員變量CREATOR,這個變量需要實現(xiàn) Parcelable.Creator 接口。?

3.在使用內存的時候,Parcelable 類比Serializable性能高,所以推薦使用Parcelable類。4.Serializable在序列化的時候會產生大量的臨時變量,從而引起頻繁的GC。?

5.Parcelable不能使用在要將數(shù)據(jù)存儲在磁盤上的情況,因為在外界有變化的情況下Parcelable不能很好的保證數(shù)據(jù)的持續(xù)性。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容