生命周期predicament在另一个活动的方向变化生命周期、方向、predicament

2023-09-07 22:00:24 作者:我不再是你的太阳

我有一个tabhost 2活动。在活动1,我处理方向变化,并在用户活动之间切换罚款。

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

在从活动1的用户切换到活性2(通过选项卡中选择)的问题开始,进行改变方向,然后切换回活动1。我有点与发生在我的活动1,而事情的进展时,活性2可见的生命周期事件丢失。

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.

根据调试器,这里是发生在我的活动1的事件序列:

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

===取向变化===结果结果的onSaveInstanceState结果的onPause的onStop结果的onCreate结果在onStart结果onRestoreInstanceState结果onResume

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

===切换到活动2 ===结果结果的onSaveInstanceState在onPause

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

===取向的转变,而在活动2 ===结果的onStop结果的onCreate结果在onStart

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

===切返回一个从活性2 ===结果onResume

=== Switchback BACK FROM Activity2 === onResume

正如你所看到的,我有机会拯救我的活动1中的数据调用时的onSaveInstanceState它切换到活性2,但我从来没有得到一个电话onRestoreInstanceState恢复它。

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.

问题

为什么android的呼吁切换到另一个活动的onSaveInstanceState我如果不打算叫onRestoreInstanceState当切换回?

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

为什么我没有拿到的onSaveInstanceState / onRestoreInstanceState在我的活动1,而活性2可见?我仍然必须保存/恢复数据是否是可见或不可见,是吗?

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?

哪里是数据备份/恢复在这种情况下,最安全的地方?如果它的onSaveInstanceState中/ onRestoreInstanceState不是,我怎么进入包进行恢复?

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?

感谢您的帮助!

格雷格

推荐答案

这是一个非常好的问题。

This is a really good question.

综观文档的onSaveInstanceState为/ onRestoreInstanceState他们的目的(因此默认实现的行为)是保存它有一个ID的任何视图的视图状态。实施例中,如果一个复选框被选中,则选择该单选在RadioGroup中等等。

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.

什么是在文档还指出的是,捆绑创建用于保存的onSaveInstanceState传递/双方的onCreate和onRestoreInstanceState(关于这一点在某一时刻)时是活动重新设置。

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'.

我猜是因为活动1在旋转时隐藏,onRestoreInstanceState不叫,因为有(看即,它不能被看作/)没有视图。此外,它是完全有可能有肖像/ 2完全不同的布局文件景观这可能有不同的ID不同的UI元素。

I'm guessing that because Activity 1 is hidden at the time of rotation, onRestoreInstanceState isn't called because there is no 'view' (i.e., 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.

这样一来,我会说,如果你想使用捆绑的onSaveInstanceState中保存自己的数据,那么你需要添加额外的逻辑在你的onCreate(在活动1)有处理自己的数据(以及在onRestoreInstanceState有条件这样做)。

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).

在特别的,你可以这样的onCreate知道它需要,因为方向已经改变,而不是依靠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.

这有任何意义吗?这是唯一的办法,我可以跨preT的逻辑。

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