Nested parcelable object not reading

后端 未结 1 1955
轮回少年
轮回少年 2021-02-01 04:05

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 wo

1条回答
  •  轮回少年
    2021-02-01 04:29

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

    0 讨论(0)
提交回复
热议问题