Modge February 2016

Saving and restoring an ArrayDeque onSaveInstanceState

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.

Answers


Pablo Rivero February 2016

You can do this:

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

cast again when restoring.

hope it helps

Post Status

Asked in February 2016
Viewed 3,518 times
Voted 9
Answered 1 times

Search




Leave an answer