To serialize an object by implementing the Parcelable interface:
1, realize parcelable interface.
2, and implement the public Parcelable interface
void Writetoparcel (Parcel dest, int flags) method.
3. A custom type must contain a static member named Creator, which requires the implementation of the Parcelable.creator interface and its methods.
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
+
"]"
;
}
}
|
Passing entity classes:
Bundle bundle = new bundle ();
Bundle.putserializable (title, title);
Bundle.putparcelable (title, title);
Context.startactivity (New Intent (context, target). Putextras (bundle));
Receive:
Mcollectparamobj = (collectparamobj) getintent (). Getparcelableextra (
"title");
Android activity passing entity class object