0

So I'm trying to send an object from one activity to another as Parcelable but the object on receiver side is always null. The object is filled completely on the sender side so I'm almost certain it has to do something with reading the object.

Here's the Parcelable object:

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

public class Picture implements Parcelable {

    public String pictureID, title, year, price, author;
    public Bitmap picture;

    public Picture(){
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(pictureID);
        dest.writeString(title);
        dest.writeString(year);
        dest.writeString(price);
        dest.writeString(author);
        dest.writeParcelable(picture, flags);
    }

    protected Picture(Parcel in) {
        pictureID = in.readString();
        title = in.readString();
        year = in.readString();
        price = in.readString();
        author = in.readString();
        picture = in.readParcelable(Bitmap.class.getClassLoader());
    }

    public static final Parcelable.Creator<Picture> CREATOR = new Parcelable.Creator<Picture>() {
        public Picture createFromParcel(Parcel source) {
            return new Picture(source);
        }
        public Picture[] newArray(int size) {
            return new Picture[size];
        }
    };
}

Sender activity:

Picture firstPic = new Picture();
firstPic.pictureID = "1";
firstPic.title = "Mona Lisa";
firstPic.author = "Leonardo Da Vinci";
firstPic.price = "99999000";
firstPic.year = "2250";
firstPic.picture = BitmapFactory.decodeResource(getApplication().getResources(),R.drawable.monalisa);
Intent i = new Intent(getApplicationContext(),MainScreen.class);
i.putExtra("first",firstPic);
startActivity(i);
finish();

Receiver activity:

Picture currentPicture = new Picture();
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    currentPicture = intent.getParcelableExtra("first");
}

EDIT: I've checked all the other topics about Parcelable and I've also followed this tutorial and I can't really find the differences where the error might be.

EDIT 2: Problem solved. Everything was working fine since the beginning but for some reason I've put some more code in between filling object with data and putExtra so basically I was sending an empty object ¯\_(ツ)_/¯

DisplayName
  • 195
  • 1
  • 16

1 Answers1

0

First avoid pass Bitmap's, they can be too large and extract string "first" to a constant to ReceiverActivity class:

public class ReceiverActivity extends ... {
    public static final String EXTRA_FIRST = "first";
    ...
} 

So:

i.putExtra(ReceiverActivity.EXTRA_FIRST,firstPic);

And

currentPicture = intent.getParcelableExtra(ReceiverActivity.EXTRA_FIRST);

This strange thing already happened with me, i solved it using this way.

betorcs
  • 2,771
  • 22
  • 28