Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practices on Android to keep data between activities deathes/restarts for the whole application session?

We're designing an Android app that has several activities which are working in a wizard like way - user should pass from the activity #1 to activity #5 to get to the final activity (#6).

Since we know an activity can be suddenly terminated by OS on low memory we used Application class as a static storage for keeping the data the user inputs on "wizard" activities and other data our app needs for the whole session.

Unfortunately we've discovered this approach fails - looks like the Application class instance is also can be killed by OS (this was specifically discovered on Android 1.6 versus 1.5). Are our expectations wrong on this approach (we think Application class instance always lives for the whole app session)?

So the question is - what is the best practices on Android to keep data between activities deathes/restarts for the whole application session?

like image 977
Vit Khudenko Avatar asked Jan 09 '10 10:01

Vit Khudenko


1 Answers

We're designing an Android app that has several activities which are working in a wizard like way - user should pass from the activity #1 to activity #5 to get to the final activity (#6).

Are you sure you should be implementing them as separate activities? Why not a single activity, using ViewFlipper or something to move between the wizard states?

Are our expectations wrong on this approach (we think Application class instance always lives for the whole app session)?

The Application class instance does always live for the whole app session, AFAIK. Once all your activities are destroyed, though, the Application will be destroyed as well.

So the question is - what is the best practices on Android to keep data between activities deathes/restarts for the whole application session?

  1. Use a Service -- if this gets kicked out of RAM, just start over from scratch
  2. Use a database
  3. Use a file

Or, best: don't use multiple activities in this case. A wizard is a single logical thing; treat it as such and make it be a single activity. Persist your state via onSaveInstanceState(), and you're set.

like image 124
CommonsWare Avatar answered Nov 03 '22 00:11

CommonsWare