views:

198

answers:

2

Hi there,

My question is about restoring complex activity related data when coming back to the activity using the "back" button". Activity A has a ListView which is connected to ArrayAdapter serving as its data source - this happens in onCreate().

  1. By default, if I move to activity B and press "back" to get back to activity A, does my list stay intact with all the data or do I just get visual "copy" of the screen but the data is lost?
  2. What can I do when more than activities are involved? Let's say activity A starts activity B which starts activity C and then I press "back" twice to get to A. How do I ensure the integrity of the A's data when it gets back to the foreground? PrefsManager does not seem to handle complex object very intuitively.

Thanks, Rob

A: 

Unless you actively finish() Activity A when you start Activity B, its data and state will all be present when you return to it, regardless of how many activities you stack up.

David Hedlund
That is not necessarily true. Android may elect to destroy the activity to free up RAM for other processing.
CommonsWare
Thank you for your reply, David.
Rob
+1  A: 

Activity A has a ListView which is connected to ArrayAdapter serving as its data source

Note that an ArrayAdapter is not a persistent store.

By default, if I move to activity B and press "back" to get back to activity A, does my list stay intact with all the data or do I just get visual "copy" of the screen but the data is lost?

That depends.

Typically, Activity A has not gone anywhere. However, if the user leaves the app for an extended period, Android may destroy Activity A, leaving a placeholder in the activity stack, to free up memory. If, later, the user presses BACK, Android will re-create Activity A. You need to hang onto your data in this case, either by using a persistent store (file, database, etc.) or by using onSaveInstanceState() and onRestoreInstanceState().

You can find more in the Activity Lifecycle section of the Activity class description.

CommonsWare
Thanks a lot for your reply.
Rob