4

I am developing an Android application consisting of over 10 activities. I have some state objects that I access in almost every activity and for this purpose these were implemented as global static variables in the MyApplication class.

I noticed that this approach is OK as long as the user is "in" the application. However, when he presses the home button and opens another apps and then goes back to my app through the "Recent activities" button, I see that the Android system resets the statics from the MyApplication so I have to deal with NullPointerExceptions. I know that this behaviour is caused by Android killing and recreating the application process.

I know that the best way to persist this kind of data is using SharedPreferences or SQLite, and I have no problem checking if MyState==null in the onCreate for and restoring it, but the problem is that I don't know when to properly store my state object (in prefs or database). I tried to override MyApplication's finalize() - no good, I saw that onLowMemory may not be called, I don't see how can I use onPause, OnStop and so on because I have so many activities that the serialization de-serialization would considerably slow down the app.

Any thoughts? Thanks in advance!

rrdev
  • 115
  • 1
  • 12
  • If data is not huge, make your persistence Object as Singleton. – Ganesh K Oct 09 '12 at 07:28
  • So when the Android OS will recreate the application process, therefore recreating the Application class this Object will restore its state? – rrdev Oct 09 '12 at 07:59
  • Restarting/Recreation of application process depends on several factors like memory, no of apps in live, no of services currently running.etc. So save your data in your main activity on destroy or app on Destroy. – Ganesh K Oct 09 '12 at 08:06
  • Good point, but according to documentation here [link](http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29) the general advice is to: "do not count on this method being called as a place for saving data". From what I understand there is no guarantee that this method will ever get called. – rrdev Oct 09 '12 at 08:15
  • That is true, but happens very rarely. If you want more control, then save data at appropriate times in your activities on Pause, on saved Instance Methods. – Ganesh K Oct 09 '12 at 08:21
  • Thanks. I think I will choose something in the middle: save critical data in onDestroy and restore it when needed, and if it doesn't get called (which happens very rarely) then simply restart the application if the State object is null. – rrdev Oct 09 '12 at 08:40

1 Answers1

2

It is better to not depend on the Application class unless you need to load some data, before anything else is started. Android can kill your process at any time to free resources, so your app should be able to handle this. Save all of your data in a snigleton class, and load it lazily -- check for null, and if so load on first access. It the state needs to be persistent, consider staving it file/shared prefs. If not, your app can probably live without it, so just make sure you check for null, etc.

Generally, you should persist state when activities become inactive -- onStop(), onPause(), but you can save as soon as it makes sense (e.g., the user has entered all required data). Spin off an AsyncTask to save data in the background and let the user continue their work.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • It all makes sense, but here we are talking about an app having about 10 screens of inputs (the user is asked to enter the data). All the information is stored in some state objects which are used to perform some tasks at the end of this flow. Is this the best practice to run some sort of SaveAsyncTask (which will save the data in files/prefs etc) every time one of this activities goes onPause or onStop? – rrdev Oct 09 '12 at 10:43
  • If you have 10 screens you should probably review your design. If you want the user to be able to pick up where they left off, it would be a good idea to save state on each screen transition. – Nikolay Elenkov Oct 09 '12 at 12:13
  • 1
    @NikolayElenkov Where do you hang/declare your singleton class? What should you use your Application class for? – powder366 Apr 18 '16 at 17:19