开发者

Android: How to restore List data when pressing the "back" button?

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, 开发者_Python百科Rob


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜