I\'d like to make class A Parcelable.
public class A {
public String str;
public ArrayList list;
}
This is what I\'ve come
If you have only one Parcelable object inside your main Parcelable object, not list like the accepted answer case. Then it will be like the following:
Class A
public class A implements Parcelable {
public String str;
public B objectB;
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//The parcelable object has to be the first one
dest.writeParcelable(objectB, flags);
dest.writeString(str);
}
private A(Parcel in) {
this.objectB = in.readParcelable(B.class.getClassLoader());
str = in.readString();
}
public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
public A createFromParcel(Parcel in) {
return new A(in);
}
public A[] newArray(int size) {
return new A[size];
}
};
}
Class B
public class B implements Parcelable {
public String str;
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(str);
}
private B(Parcel in) {
str = in.readString();
}
public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
public B createFromParcel(Parcel in) {
return new B(in);
}
public B[] newArray(int size) {
return new B[size];
}
};
}
IMPORTANT:
Please note that the order that you write and read the Parcelable object matters. Checkout this answer for more details