Life cycle predicament with orientation change in another activity

I've got 2 activities in a tabhost. In Activity1, I handle orientation changes and when the user switches between activities fine.

The problem starts when the user switches from Activity1 to Activity2 (via tab select), performs an orientation change, then switches BACK to Activity1. I get a bit lost with the life cycle events that take place in my Activity1 while things are going when Activity2 is visible.

According to the debugger, here's the sequence of events that occur in my Activity1:

=== Orientation Change ===
onSaveInstanceState
onPause
onStop
onCreate
onStart
onRestoreInstanceState
onResume

=== Switch TO Activity 2 ===
onSaveInstanceState
onPause

=== Orientation Change WHILE IN Activity 2 ===
onStop
onCreate
onStart

=== Switchback BACK FROM Activity2 ===
onResume

As you can see, I have opportunity to save my Activity1 data in the call to onSaveInstanceState when it's switched out to Activity2, but I never get a call to onRestoreInstanceState to restore it.

Questions

  • Why does android call my onSaveInstanceState upon switching to another activity if it doesn't intend to call onRestoreInstanceState when it switches back?

  • Why don't I get onSaveInstanceState/onRestoreInstanceState in my Activity1 while Activity2 is visible? I must still save/restore data whether it's visible or not, yes?

  • Where is the safest place to save/restore data in this case? And if it's NOT in onSaveInstanceState/onRestoreInstanceState, how do I access the bundle for restoration?

  • Is there another solution like other callbacks I can take advantage of to alleviate this?

  • Thanks for any help!

    Greg


    This is a really good question.

    Looking at the docs for onSaveInstanceState/onRestoreInstanceState their purpose (and therefore behaviour of the default implementations) is to save view state of any view which has an ID. Examples, if a checkbox is checked, which radiobutton in a radiogroup is selected and so on.

    What is also stated in the docs is that the Bundle created/saved for onSaveInstanceState is passed to both onCreate and onRestoreInstanceState (more on this in a moment) when the Activity is 're-instated'.

    I'm guessing that because Activity 1 is hidden at the time of rotation, onRestoreInstanceState isn't called because there is no 'view' (ie, it can't be seen/viewed). Also, it is entirely possible to have 2 completely different layout files for portrait/landscape which potentially have different UI elements with different IDs.

    As a result, I'd say if you want to use the Bundle in onSaveInstanceState to save your own data, then you need to add extra logic in your onCreate (in Activity 1) to process your own data there (as well as doing it conditionally in onRestoreInstanceState).

    In particular, you could maintain a 'last known' orientation field so that onCreate knows that it needs to process your own data because orientation has changed, rather than relying on onRestoreInstanceState being called.

    Does that make any sense? It's the only way I can interpret the logic.


    It's perfectly fine to restore state via onCreate() , just be sure to perform a null check as the bundle will be null whenever there isn't any saved state. As for onRestoreInstanceState() , it is only called if the activity is killed. I like to think of it as performing a backup of the application state, just in case it's needed.

    See Ogre_BGR 's answer here: onSaveInstanceState () and onRestoreInstanceState ()

    链接地址: http://www.djcxy.com/p/46440.html

    上一篇: 如何在方向更改时保留文本数据

    下一篇: 生活周期困境与另一项活动中的方向变化