简体   繁体   中英

Activity being destroyed?

Very new to Android but when I am, say, in activity A, and I intend to move to activity B, then go back to activity A, all the data displayed in activity A before going to to B is lost. I never call finish(), am I doing something wrong?

I recommend going through Google's training and learning about activity life cycles.

http://developer.android.com/training/basics/firstapp/index.html

With your two activities, override all of the life cycle methods (onCreate, onResume, etc) and put a simple log message in them. Just move between the activities, and watch your log to learn the order of events.

From there, learn about Bundles and how to save your instance states and you'll be well on your way to making apps!

Check that all your Views do have identifiers in your XML. Android restores only those views which have identifiers.

Additionally, for your custom data (not views):

It can be that your Activity A got destroyed while in background.

Normally they do like that:

1) Add onSaveInstanceState() to your Activity. There put all your custom data in the Bundle argument. Be sure call super at the end . You do not need to store your views, just your custom data such as your custom variables etc.

2) in onCreate see if the savedInstanceState Bundle argument is null.

3) If null populate with your default data.

4) If not null restore the data from the Bundle and populate with them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM