Life cycle difficulty with a change in orientation in another activity

I have 2 actions in tabhost. In Activity1, I handle orientation changes and when the user switches between actions.

The problem starts when the user switches from Activity1 to Activity2 (by selecting a tab), performs a change in orientation, then switches BACK to Activity1. I get a little lost in the life cycle events that occur in my Activity1, while everything happens when Activity2 is visible.

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

=== Change orientation ===
onSaveInstanceState
Onpause
Onstop
Oncreate
Onstart
onRestoreInstanceState
onResume

=== Switch to operation 2 ===
onSaveInstanceState
onPause

=== Change orientation WHILE IN Activity 2 ===
Onstop
Oncreate
onStart

=== Disable BACK FROM Activity2 ===
onResume

As you can see, I have the ability to save Activity1 data in an onSaveInstanceState call when it switched to Activity2, but I never get an onRestoreInstanceState call to restore it.

Questions

  • Why does android call my onSaveInstanceState when switching to another action, if it does not intend to call onRestoreInstanceState when it returns?

  • Why don't I get onSaveInstanceState / onRestoreInstanceState in my Activity1 while Activity2 is visible? I still have to save / restore data, visible or not, right?

  • Where is the safe place to save / restore data in this case? And if it's NOT in onSaveInstanceState / onRestoreInstanceState, how can I access the recovery package?

  • Is there any other solution like other callbacks that I can use to facilitate this?

Thanks for any help!

Greg

+6
source share
2 answers

This is a really good question.

Looking at the documents for onSaveInstanceState / onRestoreInstanceState, their purpose (and therefore the default behavior of the implementations) is to preserve the view state of any kind with an identifier. Examples, if the checkbox is selected, which beacon in the radio group is selected, etc.

What is also indicated in the documents is that the package created / saved for onSaveInstanceState is transmitted both onCreate and onRestoreInstanceState (more about this at once) when the activity is "re-set".

I assume that since action 1 is hidden during rotation, onRestoreInstanceState is not called because there is no โ€œviewโ€ (ie it cannot be seen / viewed). In addition, it is quite possible to have 2 completely different layout files for a portrait / landscape, which could potentially have different user interface elements with different identifiers.

As a result, I would say if you want to use the Bundle in onSaveInstanceState to save your own data, then you need to add additional logic to your onCreate (in step 1) to process your own data there (just like conditionally in onRestoreInstanceState).

In particular, you could save the field of the "last known" orientation, so that onCreate knows that it needs to process your own data, because the orientation has changed, and not rely on the called onRestoreInstanceState.

Does that make sense? This is the only way to interpret logic.

+2
source

It is great to restore state using onCreate() , just make sure you do a null check as the package will be null when there is no saved state. As for onRestoreInstanceState() , it is called only if the activity is killed. I like to think of it as backing up the application state if necessary.

See Ogre_BGR here: onSaveInstanceState () and onRestoreInstanceState ()

0
source

Source: https://habr.com/ru/post/886497/


All Articles