标签:
通过实现Parcelable接口序列化对象的步骤:
1、实现Parcelable接口。
2、并且实现Parcelable接口的public
void writeToParcel(Parcel dest, int flags)方法 。
3、自定义类型中必须含有一个名称为CREATOR的静态成员,该成员对象要求实现Parcelable.Creator接口及其方法。
public class Person implements Parcelable { public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() { @Override public Person[] newArray( int size) { return null ; } @Override public Person createFromParcel(Parcel source) { Person result = new Person(); result.age = source.readInt(); result.name = source.readString(); result.address = source.readParcelable(Address. class .getClassLoader()); return result; } }; private Address address; private String name; private int age; @Override public int describeContents() { return 0 ; } public Address getAddress() { return address; } public void setAddress(Address address) { this .address = address; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(age); dest.writeString(name); dest.writeParcelable(address, PARCELABLE_WRITE_RETURN_VALUE); } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } @Override public String toString() { return "Person [address=" + address + ", name=" + name + ", age=" + age + "]" ; } } |
传递实体类:
Bundle bundle = new Bundle();
// bundle.putSerializable(TITLE, title);
bundle.putParcelable(TITLE, title);
context.startActivity(new Intent(context, target).putExtras(bundle));
接收:
mCollectParamObj = (CollectParamObj) getIntent().getParcelableExtra(
"title");
标签:
原文地址:http://www.cnblogs.com/zhaoleigege/p/4997090.html