让活动在轮换时被销毁有什么好处? [英] What is the advantage of letting an activity be destroyed on rotation?

查看:15
本文介绍了让活动在轮换时被销毁有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种方法我都用过:

  1. 让活动在旋转时被销毁
  2. 不要让活动在轮换时被破坏

我的方法几乎每次都是捕捉旋转事件,如果需要,调用 setContentView 并再次添加一些组件.如果没有,就让它旋转,布局会自动适应.

My approach almost everytime is to catch the rotation event and if needed call the setContentView and add some components again. If not, just let it rotate and the layouts are designed to adapt.

到目前为止,我只看到在具有非常复杂的动态结构的屏幕上销毁它的优势,并且每当我旋转而不是销毁时,在重新构建屏幕时会显示一些闪烁.

So far I only have seen advantages on letting it be destroyed on screens with very complex construction that are very dynamic, and whenever I rotate and not destroy show some flickering when re-building the screen.

必须使用 onSaveInstance 传递状态的开销,onRestoreInstace 有时很容易出错,并且在某种程度上很耗时.

The overhead of having to pass the state with onSaveInstance, onRestoreInstace is sometimes very error prone, and somehow time consuming.

我错过了什么吗?

更新:

我没有在我的代码上执行任何类型的 if "Orientation.XPTO == ...".这是两种方法中每一种方法的逻辑(代码被重用):

I'm not doing any kind of if "Orientation.XPTO == ..." on my code. This is the logic of each of the 2 approaches (the code is reused):

破坏时

onCreate -> DrawUI() setContentView and add views -> fill() add content

未销毁时:

onCreate -> DrawUI() setContentView and add views -> fill() add content
onRotation -> DrawUI() setContentView and add views -> fill() add content

在旋转后调用 setContentView 时,它会为设备方向选择正确的布局(查看 Google 的 Reto Meier 的回答 https://stackoverflow.com/a/456918/327011)

When calling setContentView after rotation it will pick the right layout for the device orientation (Check this answer by Google's Reto Meier https://stackoverflow.com/a/456918/327011 )

DrawUI 和填充必须具有纵向和横向布局的逻辑,因为可以在两个方向中的每一个上创建活动.

And the DrawUI and fill would have to have the logic for both the portrait and landscape layouts as the activity can be created on each of the two orientations to begin with.

推荐答案

我错过了什么吗?

Am I missing something?

是的.您假设您的替代方案不太容易出错.

Yes. You are assuming that your alternative is somehow less error prone.

通过不经历销毁和重新创建周期,您必须确保为每个可能的配置更改处理更改每个资源.

By not going through the destroy-and-recreate cycle, you have to ensure that you are handling changing every resource for every possible configuration change.

不要让活动在旋转时被破坏

Don't let the activity be destroyed on rotation

除非您使用 android:screenOrientation 来强制您的活动进入单一方向(例如,landscape),否则您不能只处理与旋转相关的配置更改.您需要处理所有配置更改.否则,一旦用户将他们的设备放入 Dock、将其从 Dock 中移除、从设置"更改语言、附加或分离键盘、更改全局字体缩放等,您的应用就会中断.

Unless you are using android:screenOrientation to force your activity into a single orientation (e.g., landscape), you cannot only handle rotation-related configuration changes. You need to handle all configuration changes. Otherwise, as soon as the user drops their device into a dock, removes it from a dock, changes language from Settings, attaches or detaches a keyboard, changes the global font scaling, etc., your app will break.

反过来,这意味着在每次配置更改时,您需要:

This, in turn, means that on every configuration change, you need to:

  • 为可能的新字符串资源更新 UI
  • 调整或重新加载您的布局(通过调整",包括更改任何可绘制对象、动画、菜单等)
  • 与您的资源相关的任何其他内容(例如,您的 PreferenceFragment 中的数组列表)
  • update your UI for your potentially new string resources
  • adjust or reload your layouts (and by "adjust" that includes changing any drawables, animations, menus, etc.)
  • anything else tied to your resources (e.g., array lists in your PreferenceFragment)

问题是你会忘记一些东西.例如,您将错过更改与操作栏项关联的字符串,因此现在您的大部分 UI 都是西班牙语,而操作栏项是英文.你会忘记的那些事情会变得不那么明显(你多久测试一次你的西班牙语翻译?).

The problem is that you are going to forget something. For example, you will miss changing a string associated with an action bar item, so now most of your UI is in Spanish and that action bar item is in English. The sorts of things you are going to forget will be less obvious (how often do you test your Spanish translation?).

这篇关于让活动在轮换时被销毁有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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