1.製作Person類別 並 implemens Parcelable介面
2.定義屬性name等等..
3.實作Parcelable內的方法
protected Person(Parcel in) {
name = in.readString();
id = in.readInt();
address = in.readString();
tel = in.readString();
}
public static final Creator<Person> CREATOR
= new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(id); dest.writeString(address); dest.writeString(tel); }
4.在A頁面宣告 Person p;
5.產生新物件
//對應Person屬性填入資料
p = new Person("Candy", 1 ,"taipei" , "02333666");
6.產生Bundle物件,並放入Person物件p
Bundle bundle = new Bundle();
bundle.putParcelable("Candy", p);
intent.putExtras(bundle);
7.B頁面取得資料 bundle.getParcelable("Candy");
Intent intent = this.getIntent();
Bundle bundle =intent.getExtras();
Person p = bundle.getParcelable("Candy");
System.out.println("名字"+ p.getName());
全站熱搜