3

I have an ArrayList<> of a custom Object that I need to save when my users close down my app. I can see in logcat that it "saves" it to outBundle when I use Bundle.putParcelableArrayList, but it doesn't recover it when I start the app again, either with Genymotion or physical phone. I also can't cast the final String to Serializable for whatever reason if that would help.

static final String tasks = "tasks";

outState.putParcelableArrayList(tasks, taskList); //this works

taskList = savedInstanceState.getParcelableArrayList(tasks); //this doesn't

I've tried calling it in onCreate with a (savedInstanceState != null) statement and in onRestoreInstanceState. Am I missing something obvious here?

EDIT: this is what I want to see in logCat:

outState.putParcelableArrayList(tasks, taskList);
        System.out.println("saved: " + taskList);

And this is what I get:

04-15 09:24:27.513    1791-1791/com.aau.uni.mea2015.a317b.cueless I/System.out﹕ saved: [this is a task
04-15 09:24:27.513    1791-1791/com.aau.uni.mea2015.a317b.cueless I/System.out﹕ 0 - 1 minutter, 1 underopgave(r)]

There is no logcat output for onRestore or onCreate because it never "plays" it. /edit

  • Is it crashing? What is the logcat output? – Anyonymous2324 Apr 15 '15 at 09:22
  • 1
    Do an edit, we cannot read code output in comments – Dimitri Apr 15 '15 at 09:26
  • @OliverGHjermitslev Please delete your comment; always update your question when asked for me details. – GhostCat Apr 15 '15 at 09:26
  • What do you mean by "closing down" the app? If you completely remove the app from the task manager, the saved instance state will also be gone. Are you implementing `Parcelable` properly? Have you debugged, what is returned exactly? – Blacklight Apr 15 '15 at 09:26
  • Please elaborate, because when you *close* your app, you will **not** get back your instance state. Instance state is only restored when the OS re-creates your activity after temporarily (due to memory constraints and the like) deactivating your app, not when your app is started again after being closed properly before. – dhke Apr 15 '15 at 09:32
  • In that case I have it all wrong. I guess at least I have saved my app from memory leaks. – Oliver G Hjermitslev Apr 15 '15 at 09:33
  • As Blacklight said, savedInstanceState will be removed if your app is completly removed (low memory space, user removes/stops app from manager etc). If you need for settings to persist between restarts of your app, try storing the arralist in sharedPreferences. – Evripidis Drakos Apr 15 '15 at 09:33

1 Answers1

1

From what I read here "... that I need to save when my users close down my app. ... but it doesn't recover it when I start the app again, either with Genymotion or physical phone." it sounds like you are closing the app manually.

This way onRestoreInstanceState() won't be called. It is only called if the activity has been killed by the OS.

Check this post or this SO post as well.

Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60