Search code examples
javaandroidparcelable

Problems with parcelable custom object - Android


I am trying to pass a custom object from one activity to another one. I found that we can use a bundle to pass data into the intent and that we need a parcelable interface implemented in our class.

In the following code I removed the useless stuff.

public class TravelCard implements Parcelable {

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

    @Override
    public TravelCard[] newArray(int size) {
        return new TravelCard[size];
    }
};
private String travelTitle, travelCountry, dateRange, numOfPerson;
private List<TravelDay> days;

protected TravelCard(Parcel in) {
    travelTitle = in.readString();
    travelCountry = in.readString();
    dateRange = in.readString();
    numOfPerson = in.readString();
    in.readTypedList(days, TravelDay.CREATOR);
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.travelTitle);
    dest.writeString(this.travelCountry);
    dest.writeString(this.dateRange);
    dest.writeString(this.numOfPerson);
    dest.writeTypedList(this.days);
}

In this class I have a List of another custom object:

public class TravelDay implements Parcelable {

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

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

public int current_day;
private String title, note, dateOfToday;

protected TravelDay(Parcel in) {
    title = in.readString();
    note = in.readString();
    dateOfToday = in.readString();
    current_day = in.readInt();
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(title);
    dest.writeString(note);
    dest.writeString(dateOfToday);
    dest.writeInt(current_day);
}

I think until here everything should be fine.

In my activity :

Intent intent = new Intent(MainActivity.this, TravelDayActivity.class);
Bundle data = new Bundle();
data.putParcelable(DataKey.TRAVEL_CARD_BUNDLE,item);
intent.putExtras(data);
startActivity(intent);

And then in the TravelDayActivity :

Intent intent = getIntent();
Bundle data = intent.getExtras();
TravelCard travelCard = data.getParcelable(DataKey.TRAVEL_CARD_BUNDLE);

Every time I tried to access the TravelDayActivity the app stop running. I used the debug mode to search something wrong but I did not find anything. Thank you in advance

EDIT : In the MainActivity that's how I get the item variable:

mAdapter = new TravelCardAdapter(travelCards, new TravelCardAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(TravelCard item) {
            Intent intent = new Intent(MainActivity.this,TravelDayActivity.class);
            Bundle data = new Bundle();
            data.putParcelable(DataKey.TRAVEL_CARD_BUNDLE,item);
            intent.putExtras(data);
            startActivity(intent);
        }
    });

Whenever I click on one of the recycler view item this code above is executed.


Solution

  • After the suggestion of @BVantur, I solved my problem using the parceler library : parceler library

    It has been very helpful, easy to use and a good documentation! I highly recommend it.