28

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();
    }

}
iomartin
  • 3,149
  • 9
  • 31
  • 47

1 Answers1

75

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();
}
iomartin
  • 3,149
  • 9
  • 31
  • 47
  • @MohitSharma, I just changed the order in which I read/write. The `OtherClass` should be the first to be read/written from/to the `Parcel` – iomartin Nov 15 '13 at 12:59
  • 4
    why the order matters?? – Zyoo May 22 '14 at 18:21
  • 6
    @Zyoo Because a parcel must be read in the same order that it is written. Now if you have problems with the current order (I had a problem when calling writeToParcel on a Location object in case that object was null) - then change the order and make sure you also change the order in which you read. For more details, check out Android's implementation of Parcel. – AgentKnopf Jun 01 '15 at 13:28
  • @Zainodis, a sample or reference would be appreciated. Otherwise, thank you for the hint. – andrea.rinaldi Jun 22 '15 at 09:50
  • @andrea.spot You can find the default implementation guide here: http://developer.android.com/reference/android/os/Parcelable.html and the accepted answer from here http://stackoverflow.com/a/2264245/1041533 though the original android documentation does not state the issue with the order it is definitely the case – AgentKnopf Jun 22 '15 at 11:26
  • That solved my problem! Thanks! It's kind of lame that the order matters and apparently there's no information about that issue in the official docs. – Dominik Dec 02 '18 at 15:16