I'm implementing Parcelable class that has another Parcelable insde.
In OuterParcelable class:
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle tmp = new Bundle();
tmp.putParcelable("innerParcelable", mParcelable);
dest.writeBundle(tmp);
and then:
public OuterParcelable(Parcel parcel) {
super();
Bundle b = parcel.readBundle();
mParcelable = b.getParcelable("innerParcelable");
and:
public OuterParcelable createFromParcel(Parcel in) {
return new OuterParcelable(in);
}
When I recreate object using above code I get:
08-18 17:13:08.566: ERROR/AndroidRuntime(15520): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: my.package.InnerParcelable
Why are you putting the value into a Bundle? Did you completely implement the parcelable in your class?
Parcelable Skeleton
I added a few things to make null values more easily dealt with, but the principle is the same. You need the @Override items, constructor, and Creator.
If you're going to read and write a parcelable you will have issues if you specify null as the class loader.
A clean way to store non-primitive attributes as parcelable, possibly null, values. Use Parcel.writeValue() and readValue(). See comments in code below:
Works like a charm. writeValue() and readValue() encapsulate the dealing with possible nulls and type detection. From javadoc: