0

I am working on an video app thats uses an ArrayDeque to store our history of videos viewed, using push, pop. It works well but I'm looking for a clean way of saving and restoring the ArrayDeque

private Deque<ProgramModel> mVideoHistory = new ArrayDeque<>();

I save the list by saving it as an arraylist.

@Override
public void onSaveInstanceState(final Bundle outState) {
    // save history
    ArrayList<ProgramModel> history = new ArrayList<>();
    for (ProgramModel program : mVideoHistory) {
        history.add(program);
    }
    outState.putParcelableArrayList("videoHistory", history);
}

Then I restore it

if (savedInstanceState != null) {
    // restore history
    ArrayList<ProgramModel> history =
            savedInstanceState.getParcelableArrayList("videoHistory");
    mVideoHistory = new ArrayDeque<>(history);
}

Can it be done in a more efficient/cleaner way than this?

Ive tried outState.putSerializable("videoHistory", mVideoHistory.toArray()); but restoring caused me problems.

Eoin
  • 4,050
  • 2
  • 33
  • 43

1 Answers1

2

You can do this:

outState.putSerializable("videoHistory", (ArrayDeque<ProgramModel>) mVideoHistory);

cast again when restoring.

hope it helps

Pablo
  • 2,581
  • 3
  • 16
  • 31
  • thanks for the fast reply, seems my ProgramModel wont play nice this way, it gives me `Parcelable encountered IOException writing serializable object ` – Eoin Feb 08 '16 at 08:21
  • 1
    ProgramModel implements Serializable? If it does, check that all of the types attributes members of ProgramModel implement Serializable. – Pablo Feb 08 '16 at 08:29