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().
- 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?
- 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
Note that an
ArrayAdapteris not a persistent store.That depends.
Typically,
ActivityA has not gone anywhere. However, if the user leaves the app for an extended period, Android may destroyActivityA, leaving a placeholder in the activity stack, to free up memory. If, later, the user presses BACK, Android will re-createActivityA. You need to hang onto your data in this case, either by using a persistent store (file, database, etc.) or by usingonSaveInstanceState()andonRestoreInstanceState().You can find more in the Activity Lifecycle section of the Activity class description.