I have a custom ArrayList of MovieItems that I want to save and restore on screen rotation. I checked out this post - How to save custom ArrayList on Android screen rotate? - and implemented and adapted some of the codes but the saving and restoring don't seem to be working for me.
1) Saving the ArrayList:
public List<MovieItem> mItems = new ArrayList<>();
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList(MOVIE_KEY, (ArrayList<? extends Parcelable>) mItems);
super.onSaveInstanceState(outState);
}
2) Restoring the ArrayList:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
{
mItems = savedInstanceState.getParcelableArrayList(MOVIE_KEY);
}
setRetainInstance(true);
......
}
3) Making the MovieItem class Parcelable:
public class MovieItem implements Parcelable{
private static final long serialVersionUID = 1L;
private String mCaption;
private String mId;
private String mUrl;
private String plot;
private Double rating;
private String release_date;
private boolean favourite;
public MovieItem() {
}
protected MovieItem(Parcel in) {
mCaption = in.readString();
mId = in.readString();
mUrl = in.readString();
plot = in.readString();
release_date = in.readString();
favourite = in.readByte() != 0;
mTitle = in.readString();
}
public static final Creator<MovieItem> CREATOR = new Creator<MovieItem>() {
@Override
public MovieItem createFromParcel(Parcel in) {
return new MovieItem(in);
}
@Override
public MovieItem[] newArray(int size) {
return new MovieItem[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(mCaption);
out.writeString(mId);
out.writeString(mUrl);
out.writeString(plot);
out.writeDouble(rating);
out.writeString(release_date);
}