配置发生更改(方向更改)并破坏了活动-这是它应该工作的方式吗? [英] Configuration changed (orientation change) and destroying Activities - is this the way it's supposed to work?

查看:79
本文介绍了配置发生更改(方向更改)并破坏了活动-这是它应该工作的方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解了Android如何处理配置更改"-通过销毁活动的Activity.

我真的很想从Android Team知道为什么.我会很高兴对推理的进行方式做一个解释,因为我不理解.正如我所见,它以这种方式发挥作用的事实使我们所有人陷入痛苦的世界.

让我们假设您有一个活动,其中显示了许多EditText:s,复选框等.如果用户开始用文本/数据填充该表单,然后更改方向(或获取电话),则用户所做的所有输入离开了.我还没有找到任何保留状态的方法.这迫使我们进行极其痛苦的编码,以至于不会丢失所有数据.

如我所见,您需要另一个非活动"类(或价值持有"类),每个表单元素"(EditText,复选框等)都具有一个字段.

对于存在的每个表单元素",您都需要附加一个事件,例如"onChanged"(或onTextChanged或类似的东西),以更新"value-holding"类中的相应字段确保为您键入的每个单个字符(例如在EditText中)一次保存一次.

也许您可以使用某些侦听器(例如"onDestroy"之类的东西),然后用数据填充值保持类.

我还找到了这条信息,其中他们谈论使用Bundle, onSaveInstanceState onRestoreInstanceState ,但这还意味着程序员必须手动保存,然后将值放回正确的位置?这种方法比上面我的建议要少一些麻烦,但仍然不是很好.

有人可以告诉我我完全错了,这不是它的工作方式,我完全错过了一些重要信息吗?

解决方案

您应该阅读应用程序基础知识(特别是活动生命周期).由于Activity必须能够随时处理由于内存冲突等原因而被杀死的情况.因此,这是一种处理轮换的更干净的方法,而又不会增加过多的复杂性-而不是检查每个资源的替代资源,而是重新构造布局等.您只需保存必要的数据,终止活动,重新创建活动,然后将数据加载回(如果您愿意处理自己管理的额外复杂性,则可以使用onConfigurationChanged来这也鼓励更好的实践-开发人员必须准备好杀死他们的Activity来改变方向,这具有(好的)结果就是准备好被内存约束杀死./p>

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

I really want to know from Android Team why this is. I would appreciate an explanation on how the reasoning went, because I don't understand it. The fact that it acts in that way puts us all, as I see it, in a world of pain.

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 haven't found any way to preserve state. That forces us to make extremely painful coding to not lose all data.

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

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.

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

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.

Can someone tell me that I am totally wrong and that this is not how it works and that I totally missed some vital information?

解决方案

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.

这篇关于配置发生更改(方向更改)并破坏了活动-这是它应该工作的方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆