Nested parcelable object not reading

android, parcelable

Solution

I solved this by changing the order in which I write/read from the Parcel. I made the `dest.writeParcelable(otherClass, flags);` and the `otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader());` calls to be the first in their methods and it started working. Is that an issue?

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(otherClass, flags);
    dest.writeString(name);
    dest.writeInt(id);
}

private MyClass(Parcel in) {
    otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader());
    name = in.readString();
    id = in.readInt();
}

Problem

I'm trying to parcel an object which contains some string/int variables and an object variable. The strings and int's are working, but not the nested object. I understand I would have to it also parcelable, but I'm apparently doing something wrong=. In my nested class, the `writeToParcel` method gets called (I check with a `Log.d()` call), but the `createFromParcel()` doesn't. I end up getting a `null` object. This is my simplified code: ``` public class MyClass implements Parcelable { public MyClass() { } private Integer id; private String name; private MyOtherClass otherClass = new MyOtherClass(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public OtherClass getOtherClass() { return otherClass; } public void setOtherClass(OtherClass otherClass) { this.otherClass = otherClass; } public static final Parcelable.Creator<MyClass> CREATOR = new Parcelable.Creator<MyClass>() { public MyClass createFromParcel(Parcel in) { return new MyClass(in); } public MyClass[] newArray(int size) { return new MyClass[size]; } }; @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(id); dest.writeParcelable(otherClass, flags); } private MyClass(Parcel in) { name = in.readString(); id = in.readInt(); otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader()); } } class MyOtherClass implements Parcelable { public OtherClass() { } private String resourcePreviewURL; public String getResourcePreviewURL() { return resourcePreviewURL; } public void setResourcePreviewURL(String resourcePreviewURL) { this.resourcePreviewURL = resourcePreviewURL; } public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { Log.d("parcel", "write to parcel"); // this gets called out.writeString(resourcePreviewURL); } public static final Parcelable.Creator<MyOtherClass> CREATOR = new Parcelable.Creator<MyOtherClass>() { public MyOtherClass createFromParcel(Parcel in) { Log.d("parcel", "create from parcel"); // this doesn't get called return new MyOtherClass(in); } public ResourcePreviews[] newArray(int size) { return new ResourcePreviews[size]; } }; private OtherClass(Parcel in) { Log.d("parcel", "read from parcel"); // this doesn't get called resourcePreviewURL = in.readString(); } } ```

Original source