背景
在某些情況下究反,我們需要使用到Intent去傳遞一個自定義對象,這樣能夠減少許多不必要的代碼。查閱一下眨猎,傳遞的對象類需要實現(xiàn)Serializable接口或Parcelable接口卜高,下面我們來介紹一下如何選擇弥姻,及如何傳遞南片。
Serializable與Parcelable的選擇
Serializable是JavaSE本身就支持的,Parcelable是Android特有功能庭敦,效率比實現(xiàn)Serializable接口高效疼进,可用于Intent數(shù)據(jù)傳遞,也可以用于進程間通信螺捐。Parcelable的性能比Serializable好颠悬,在內(nèi)存開銷方面較小,所以在內(nèi)存間數(shù)據(jù)傳輸時推薦使用Parcelable定血,如activity間傳輸數(shù)據(jù)赔癌,而Serializable可將數(shù)據(jù)持久化方便保存,所以在需要保存或網(wǎng)絡傳輸數(shù)據(jù)時選擇Serializable澜沟,因為android不同版本Parcelable可能不同灾票,所以不推薦使用Parcelable進行數(shù)據(jù)持久化。
如果不使用Intent傳遞對象
發(fā)送端:
Intent intent=new Intent(this,GoodsDetailActivity.class);
intent.putExtra(Constants.INTENT_KEY_GOODS_ID,mGoodsList.get(position).getProductCode());
intent.putExtra(Constants.INTENT_KEY_GOODS_IMG_URL,mGoodsList.get(position).getProductUrl());
intent.putExtra(Constants.INTENT_KEY_GOODS_NAME,mGoodsList.get(position).getProductName());
intent.putExtra(Constants.INTENT_KEY_GOODS_PRICE,mGoodsList.get(position).getRetailPrice());
startActivity(intent);
接收端:
String goodsId=getIntent().getStringExtra(Constants.INTENT_KEY_GOODS_ID);
String imgUrl=getIntent().getStringExtra(Constants.INTENT_KEY_GOODS_IMG_URL);
String goodsName=getIntent().getStringExtra(Constants.INTENT_KEY_GOODS_NAME);
String goodsPrice=getIntent().getStringExtra(Constants.INTENT_KEY_GOODS_PRICE);
我們需要傳遞的只是mGoodsList列表中的一項茫虽,若把這一項里的各個數(shù)據(jù)分開來傳遞刊苍,不僅要定義各數(shù)據(jù)對應的常量,還寫了許多不必要的代碼濒析。
使用Intent傳遞實現(xiàn)Parcelable接口的對象
發(fā)送端
GoodsItemEntity goodsItemEntity=new GoodsItemEntity(Parcel.obtain());
goodsItemEntity.setProductName("電視機");
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("item",goodsItemEntity);
startActivity(intent);
接收端
GoodsItemEntity mGoodsItemEntity=getIntent().getParcelableExtra("item");
String productName=mGoodsItemEntity.getProductName();
當然我們傳遞的這個GoodsItemEntity需要實現(xiàn)接口Parcelable:
public class GoodsItemEntity implements Parcelable {
private String retailPrice;
private String productName;
private String productCode;
private String productUrl;
private String productId;
public String getProductCode() {
return productCode;
}
public void setProductCode(String productCode) {
this.productCode = productCode;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductUrl() {
return productUrl;
}
public void setProductUrl(String productUrl) {
this.productUrl = productUrl;
}
public String getRetailPrice() {
return retailPrice;
}
public void setRetailPrice(String retailPrice) {
this.retailPrice = retailPrice;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(retailPrice);
dest.writeString(productName);
dest.writeString(productCode);
dest.writeString(productUrl);
dest.writeString(productId);
}
public static final Creator<GoodsItemEntity> CREATOR = new Creator<GoodsItemEntity>() {
@Override
public GoodsItemEntity[] newArray(int size) {
return new GoodsItemEntity[size];
}
@Override
public GoodsItemEntity createFromParcel(Parcel in) {
return new GoodsItemEntity(in);
}
};
public GoodsItemEntity(Parcel in) {
retailPrice = in.readString();
productName = in.readString();
productCode = in.readString();
productUrl = in.readString();
productId = in.readString();
}
}
傳遞Bitmap
Intent幾乎可以實現(xiàn)你想要傳遞的各種數(shù)據(jù)正什,如傳遞基本數(shù)據(jù)類型、數(shù)組号杏、ArrayList婴氮、Bitmap、Intent盾致、Bundle等等主经,下面是傳遞Bitmap的一個例子:
發(fā)送端:
Intent intent=new Intent(activity,SecondActivity.class);
intent.putExtra("bitmap",bitmap);
activity.startActivity(intent);
接收端:
Bitmap bitmap=getIntent().getParcelableExtra("bitmap");
以上代碼均經(jīng)過驗證,有不正之處庭惜,請指正