• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ArrayList of Parcelable objects not being stored with saveInstanceState

I'm having a bit of trouble here when trying to save the state of an ArrayList of a custom object. Everytime the activity restarts, it doesn't restore the previous list.

My object is as follows,

Code:
package com.dev.shnellers.heartrate;

import android.os.Parcel;
import android.os.Parcelable;

/**
* Created by Sean on 08/01/2017.
*/

public class ReminderTime implements Parcelable {

    private int hour, minute;

    public ReminderTime(int hour, int minute) {
        setHour(hour);
        setMinute(minute);
    }

    protected ReminderTime(Parcel in) {
        hour = in.readInt();
        minute = in.readInt();
    }

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

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

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(hour);
        parcel.writeInt(minute);
    }


}

Part of my class that holds the list,
Code:
private static ArrayList<ReminderTime> reminders;
    private Bundle mBundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder_list_view);

        fab = (FloatingActionButton) findViewById(R.id.add_reminder);
        fab.setOnClickListener(this);

        // Initialize the list of reminders

        if (savedInstanceState == null || !savedInstanceState.containsKey("remind")) {
            Log.d("Reminders", "no list");
            reminders = new ArrayList<>();
        } else {
            Log.d("Reminders", "has list");
            reminders = savedInstanceState.getParcelableArrayList("remind");
        }

        mBundle = this.getIntent().getExtras();
        if (mBundle != null) {

            ReminderTime time = mBundle.getParcelable("reminderTime");
            Log.d("ReminderTime", Integer.toString(time.getHour()));
            reminders.add(time);
        }

        displayRecyclerView();
       
    }

My saveInstanceStateMethod,
Code:
@Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putParcelableArrayList("remind", reminders);
        Log.d("Reminders", "saveState");
        Log.d("Reminders list size", Integer.toString(reminders.size()));

        super.onSaveInstanceState(outState);
    }

I've looked at a number of solutions on stackoverflow but nothing seems to work.
 
When you say it "doesn't restore the previous list", what exactly happens?

Do you get the Log.d messages displayed from onSaveInstanceState()?
 
Last edited by a moderator:
Ya I get the message from onSaveInstanceState(), but its when the activity starts up again the list is empty. i.e. the saveInstanceState argument in the onCreate(), never seems to contain the key I've passed. So for instance I add a reminder which shows up, then when I add another, the previous reminder is gone.

I realize I can avoid this by storing the reminder data in the database, so can pull the list from there each time the activity starts, but seems a bit overkill to have to make calls to the DB each time.
 
That's odd, you appear to be doing everything right.
So on this line

Code:
if (savedInstanceState == null || !savedInstanceState.containsKey("remind")) {

which of these statements is true?
 
Very strange. Does the same thing happen if you try storing something simpler, like a String value?
 
Ya same thing. I ran it with a counter variable that I incremented each time a new reminder was added, but even when I stored that savedInstanceState was still null. I'ts probably best at this point I'd say to just store the data when onPause is called and retrieve it back then when she starts up again.
 
Back
Top Bottom