如果您多次使用 传递您的模型类Intent
,也许它可以实现Parcelable
接口以方便。例如:
class ProductData implements Parcelable {
protected String name;
protected String someOtherData;
public static final Parcelable.Creator<ProductData> CREATOR = new Parcelable.Creator<ProductData>() {
@Override
public ProductData createFromParcel(Parcel source) {
return new ProductData(source);
}
@Override
public ProductData[] newArray(int size) {
return new ProductData[size];
}
};
public ProductData(){
}
public ProductData(Parcel source){
readFromParcel(source);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(someOtherData);
}
public void readFromParcel(Parcel source){
name = source.readString();
someOtherData = source.readString();
}
// Here goes the rest of your model code
}
然后,您可以Activity
使用以下方法轻松地将对象传递给另一个对象Intent
:
intent.putExtra("product", productData);
然后使用
ProductData productData = intent.getParcelableExtra("product");