2

I am using Retrofit to load data from backend. The POJO implements Parcelable. I am facing issues while reading nd writing to/from the POJO. I think it's because the field name is different from what I get from backend. Here's the POJO:

        @SerializedName("poster_path")
        public String posterPath;
    ....
    private Movie(Parcel in) {
    ...
            posterPath= in.readString();
            ...

        }
...//more code
@Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(posterPath);

    }

When I get POJO through intent.getParcelableExtra, the posterPath is null. What am I doing wrong.

Sai
  • 2,089
  • 3
  • 19
  • 30

1 Answers1

2

When working with Parcelable objects, you have to read the Parcel in the exact same order that you wrote it, otherwise it won't work.

So, if you wrote it like this:

dest.writeString("blah");
dest.writeInt(1);

you have to read it like this:

str = in.readString();
someInt = in.readInt();

More info on that on this article and on this tutorial.

This question, and this one here on SO also talk about Parcelable, with examples.

Community
  • 1
  • 1
Mauker
  • 11,237
  • 7
  • 58
  • 76