Android中Intent传递对象的两种方法:Serializable & Parcelable

Android中Intent传递对象的有两种方法:
1.Bundle.putSerializable(Key,Object);
2.Bundle.putParcelable(Key, Object);

public class xx implements Serializable {

}

public class Book implements Parcelable {
private String bookName;
private String author;
private int publishTime;

public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
public Book createFromParcel(Parcel source) {
Book mBook = new Book();
mBook.bookName = source.readString();
mBook.author = source.readString();
mBook.publishTime = source.readInt();
return mBook;
}
public Book[] newArray(int size) {
return new Book[size];
}
};

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
}
}

// Bundle.putSerializable(Key,Object);
Person mPerson = new Person();
mPerson.setName("xxx");
Intent mIntent = new Intent(this, Obj.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(Key, mPerson);
mIntent.putExtras(mBoundle);

// Bundle.putParcelable(Key, Object);
Book mBook = new Book();
mBook.setBookName("Awefw");
mBook.setAuthor("xx");
mBook.setPublishTime("235");
Intent mIntent = new Intent(this, xx.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(key, mBook);
mIntent.putExtras(mBundle);
startActivity(mIntent);

posted @ 2014-05-20 15:54  petercao  阅读(2037)  评论(0编辑  收藏  举报