2

I have a singleton data manager for a specific part of my application. This data manager holds the state of a multistep process between activities. This works perfectly except when the application is placed into the background for an extended period of time as the data manager is cleared from memory. My question is what is the best way to save the state of a singleton that isn't part of one specific activity? All activity state is saved using onSaveInstanceState callback, does Application receive a callback to save state?

Bobbake4
  • 24,509
  • 9
  • 59
  • 94

2 Answers2

1

You can save the state in SharedPrefs every time the state changes, and get it from SharedPrefs every time you init the singleton

dors
  • 5,802
  • 8
  • 45
  • 71
0

I have had some similar experiences and the easiest one, straight out of my head, would be either to use the onSaveInstanceState or, persist the whole thing locally in SQLite or perhaps in the SharedPreferences, even if that might not be the right approach for this task.

If you want I can provide you with some sample code for either of the options, otherwise please check the following:

SharedPreferences :http://www.vogella.com/articles/AndroidFileBasedPersistence/article.html

SQLite: http://www.vogella.com/articles/AndroidSQLite/article.html

Saving and recieving instance state: How to use onSavedInstanceState example please

Community
  • 1
  • 1
Johan S
  • 3,531
  • 6
  • 35
  • 63