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

本文介绍在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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值