6

I want to save application state to be able to restore it after another launch. Is is it better to use method onSaveInstanceState and save it to Bundle or to use SharedPreferences?

Thanks

Waypoint
  • 17,283
  • 39
  • 116
  • 170

3 Answers3

8

It depends on your intention. Using the onSaveInstanceState() is only a reasonable solution if you want to ensure saving the state during configurations changes and other restarting events. In case you aim for a true saving of the application's state beyond the lifecycle of the application, you should consider using either the SharedPreferences or maybe even employ a database.

Till Helge
  • 9,253
  • 2
  • 40
  • 56
  • Yes, I need to be able to restore app state, after restarts occurs. So onSaveInstanceState() would be best solution – Waypoint Oct 13 '11 at 07:12
  • If the application is actually shut down by the system, the saved instance state will be lost. The same goes for shutting down the device itself. If you want to retain the state in those cases as well, `onSaveInstanceState()` will not be enough. – Till Helge Oct 13 '11 at 07:18
  • 1
    @TillHelge Actually, saveInstanceState is specifically meant to handle the case of your application being shut down by the system (e.g. due to low memory), and the savedInstanceState will be passed to the onCreate method whenever the Activity is recreated. It's only when the User finishes the Activity or the OS restarts that the state won't be restored. – bcorso Aug 16 '15 at 17:01
  • @bcorso Where are those states stored? Is it possible for a user to manually save those states + restore them without the system doing it (or in the case of a reboot, not doing it)? – Coldblackice Nov 23 '19 at 08:04
1

I may not have the same development chops as some of the other posters here (I've been seriously developing apps since July 2012), but I've found a solution that integrates SharedPreferences as well as the onSaveInstanceState().

My App has a splash screen activity that reads values from SharedPreferences and assigns them to the appropriate variables. Additionally, each Activity I make has its own onSaveInstanceState() method, and I commit all of the data I need to save to SharedPreferences there, in each and every Activity. Since onSaveInstanceState() is run before an App or Activity closes normally, it should back up data values under all normal circumstances.

It may not be the most code-efficient solution, especially in larger Apps with many Activities, but as far as my tests go it protects your app from data loss 99% of the time.

If a more experienced developer would like to chime in and confirm or deny this, I'm sure it'll enrich the question and answer.

Argus9
  • 1,863
  • 4
  • 26
  • 40
  • I know this is old, but for future readers: `onSaveInstanceState` is not run before an App or Activity closes normally. It runs before the App or Activity is shutdown by the System (e.g. orientation changes, low memory, going to background). One important case when it's not called is when a User exits the Activity by pressing the back button – bcorso Aug 16 '15 at 17:12
0

I am sure onSaveInstanceState() is the better option.

Here it is already given a better explanation: Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295