WinterMute
WinterMute

Reputation: 185

android parcelable loss object

I have problems with reaching parcelable between activities. When I inspect the object before sending it to another activity it looks fine, but after receiving it I can only see a strange id and the rest of the fields is nulled. Why does it happen? Maybe the way I do it is wrong? It would be best, you will take a look at the code parts:

This is the parcelable object:

@Data
@NoArgsConstructor
public class Scene implements Parcelable
{
    private Long id;
    private String name;
    private Light light;
    private Track effect;
    private Track music;
    private Track ambience;

    protected Scene(Parcel in)
    {
        if (in.readByte() == 0) { id = null; } else { id = in.readLong(); }
        name = in.readString();
    }

    public static final Creator<Scene> CREATOR = new Creator<Scene>()
    {
        @Override
        public Scene createFromParcel(Parcel in)
        {
            return new Scene(in);
        }

        @Override
        public Scene[] newArray(int size)
        {
            return new Scene[size];
        }
    };

    @Override
    public int describeContents()
    {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
        Map<String, Track> audio = new HashMap<>();
        audio.put(Tags.EFFECT.value(), effect);
        audio.put(Tags.MUSIC.value(), music);
        audio.put(Tags.AMBIENCE.value(), ambience);

        dest.writeLong(id);
        dest.writeString(name);
        dest.writeMap(audio);
    }
}

The scene has a map containing audio files. They are serializable (or should it be better also parcelable?) Nothing complicated, simple POJO:

@Data
public class Track implements Serializable
{
    private Long id;
    private String name;
    private String path;
    private Long duration;
    private String artist;
    private String tag;
}

And finally how I get the object:

public class AudioPlayer extends Service
[...]
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Scene scene = intent.getParcelableExtra("scene");
       [...]
    }
}

When I inspect the object by intent.setParcelableExtra(), I get this:

Scene(id=7, name=test, light=null, effect=Track(id=10, name=file.mp3, path=/some/path/file.mp3, duration=13662, artist=null, tag=effect), music=Track(id=11, name=other_file.mp3, path=/some/other/path/other_file.mp3, duration=189492, artist=null, tag=music), ambience=Track(id=12, name=another_other_file.mp3, path=/some/another/other/path/another_other_file.mp3, duration=10109, artist=null, tag=ambience))

When I inspect the object by intent.getParcelableExtra(), I get this:

Bundle[{scene=Scene(id=17179869184, name=null, light=null, effect=null, music=null, ambience=null)}]

I tried also make the Object Track parcelable but the effect was the same. Please help me solve this issue. I don´t understeand what goes wrong.

Upvotes: 1

Views: 250

Answers (1)

Ben P.
Ben P.

Reputation: 54204

When implementing Parcelable, you must guarantee that everything you "write" is exactly matched by a corresponding "read" call. Let's look at your code:

@Override
public void writeToParcel(Parcel dest, int flags)
{
    Map<String, Track> audio = new HashMap<>();
    audio.put(Tags.EFFECT.value(), effect);
    audio.put(Tags.MUSIC.value(), music);
    audio.put(Tags.AMBIENCE.value(), ambience);

    dest.writeLong(id);
    dest.writeString(name);
    dest.writeMap(audio);
}

protected Scene(Parcel in)
{
    if (in.readByte() == 0) { id = null; } else { id = in.readLong(); }
    name = in.readString();
}

Here you are writing a long, then a string, and then a map. But you are reading a byte, then conditionally a long, and then a string. You never read the map back out, and you also never write your light value.

These issues will need to be addressed. Here's what that might look like:

@Override
public void writeToParcel(Parcel dest, int flags)
{
    if (id != null) {
        dest.writeInt(1);
        dest.writeLong(id);
    } else {
        dest.writeInt(0);
    }
    dest.writeString(name);
    dest.writeSerializable(light);
    dest.writeSerializable(effect);
    dest.writeSerializable(music);
    dest.writeSerializable(ambience);
}

protected Scene(Parcel in)
{
    if (in.readInt() == 1) { 
        this.id = in.readLong();
    } else { 
        this.id = null;
    }
    this.name = in.readString();
    this.light = (Light) in.readSerializable();
    this.effect = (Track) in.readSerializable();
    this.music = (Track) in.readSerializable();
    this.ambience = (Track) in.readSerializable();
}

Upvotes: 2

Related Questions