配置更改(方向改变),并销毁活动 - 这是它应该工作的方式?这是、方向、方式、工作

2023-09-05 10:07:50 作者:放慢心跳。

好了,我读了Android上如何处理更改配置 - 通过破坏活动的活动

Ok, so I read up on how Android handles "configuration changes" - by destroying the active Activity.

第一个问题我​​真的很想知道,从Android团队 - 为什么?我想AP preciate如何推理去解释,因为我不明白=)

The first question I really want to know from Android Team - why? I would appreciate an explanation on how the reasoning went, cause I dont get it =)

在任何情况下,它以这种方式行事的事实使我们所有的,在我看来,在痛苦的世界。

In any case, the fact that it acts in that way puts us all, as I see it, in a world of pain.

让我们假设你有一个活动其中presents一些的EditText的:S,复选框等。如果一个用户开始填写该表格文本/数据,然后改变方向(或获得Phonecall),那么所有的输入用户发走了。我还没有发现任何方式preserve状态。这迫使我们做出极其痛苦编码为不失去所有数据。

Lets assume you have a Activity which presents a number of EditText:s, checkboxes etc. If a User starts to fill that form with text/data and then changes orientation (or get a Phonecall), then all input the User made is gone. I havent found any way to preserve state. That forces us to make extremely painful coding to not loose all data.

在我看来,你需要另一种非活动级(或价值持有级的可能),有一个字段为每一个表单元素(EditText上,多选等) 。

As I see it, you need another "non-Activity" class (or "value-holding" class perhaps) that has one field for each "form element" (EditText, checkbox etc).

对于每一个表单元素的存在,然后需要附加一个事件像调用onChanged(或onTextChanged或类似的东西),更新在价值持有级,以确保为每一个字符键入(在的EditText为例)保存一次。

For every single "form element" that exists, you then need to attach an Event like "onChanged" (or onTextChanged or something like that) that updates the corresponding field in the "value-holding" class to make sure that for every single character you type (in a EditText for example) is saved at once.

也许你可以使用一些监听器(如的onDestroy什么的),然后填写值持股类数据...

Perhaps you can use some listener (like "onDestroy" or something) and then fill the value-holding class with data...

我还发现这块帮助哪里他们谈论使用捆绑,的onSaveInstanceState 和 onRestoreInstanceState 的,但是这也意味着,程序员必须手动保存,再后来放回值在正确的位置?这种方法比上面我的建议有点混乱少了,但仍然不是很好......

I have also found this piece of info where they talk about using Bundle, onSaveInstanceState and onRestoreInstanceState, but that also mean that the programmer has to manually save and then later put back the values in the correct place? This approach is a bit less messier than my suggestions above, but still not very nice...

现在,有人可以告诉我,我是完全错误的,这是不是它是如何工作,而我完全错过了一些重要的信息? ; - )

Now, can someone please tell me that I am totally wrong and that this is not how it works and that I totally missed some vital information? ;-)

问候

推荐答案

您应该阅读应用程序基础(具体地,活动周期的)。由于活动 s必须是能够处理被杀害在任何时候,由于内存约束上,等它只是一个更清洁的方式来处理旋转而不会增加太多的复杂性 - 而不是检查每个资源的替代资源,重组,布局等,您只需保存您的重要数据,杀死活动,重新创建它,并加载数据回(如果你愿意来处理额外的复杂性管理这个你自己,你可以使用 onConfigurationChanged 来处理配置改变自己),这也鼓励更好的实践 - 开发人员必须prepared他们的活动被杀害的方向变化,其中有被prepared通过内存约束上也被杀死了(好)的结果。

You should read the Application Fundamentals (specifically, Activity lifecycle). Since Activitys must be able to handle being killed at any time due to memory contraints, etc. it's just a cleaner way to handle rotations without adding too much complexity - instead of checking every resource for an alternate resource, re-structuring the layout, etc. you just save your essential data, kill the activity, re-create it, and load the data back in (if you're willing to deal with the extra complexity of managing this yourself, you can use onConfigurationChanged to handle the configuration change yourself.) This also encourages better practices - developers have to be prepared for their Activity to be killed for orientation change, which has the (good) consequence of being prepared for being killed off by memory contraints also.