你可以做到Parcelable
public class MyParcelable implements Parcelable {
private Class1 object1;
private Class2 object2;
....
private ClassN objectN;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeParcelable(object1, flags)
out.writeParcelable(object2, flags)
...
out.writeParcelable(objectN, flags)
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
object1 = in.readParcelable(Class1.class.getClassLoader());
object2 = in.readParcelable(Class2.class.getClassLoader());
...
objectN = in.readParcelable(ClassN.class.getClassLoader());
}
}
我假设Class1
, Class2
, ...,ClassN
已经实现了Parcelable
。