什么是让活动进行轮换破坏的优势在哪里? [英] What is the advantage of letting an activity be destroyed on rotation?

查看:177
本文介绍了什么是让活动进行轮换破坏的优势在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了这两种方法:

  1. 让活动上旋转销毁
  2. 请不要让活动上旋转销毁

我的做法,几乎是每次捕捉轮换的事件,如果需要调用setContentView,然后再添加一些组件。如果没有,就让它旋转和布局的设计,以适应。

到目前为止,我只看到的让它可以在屏幕上非常复杂的结构是非常动态的,破坏的优点,每当我在旋转,而不是破坏演出的时候重新构建屏幕有些闪烁。

不必通过与onSaveInstance国家的开销,onRestoreInstace有时很容易出错,并以某种方式费时。

我缺少的东西?

更新:

我没有做任何假设Orientation.XPTO == ......在我的code。这是每个2的方法(在code是重复使用)的逻辑:

在销毁

 的onCreate  - > DrawUI()的setContentView并添加观点 - >填写()添加内容
 

在不被破坏:

 的onCreate  - > DrawUI()的setContentView并添加观点 - >填写()添加内容
onRotation  - > DrawUI()的setContentView并添加观点 - >填写()添加内容
 

在旋转后调用它的setContentView会挑设备的方向正确的布局(由谷歌的雷托•迈耶 HTTP检查这个答案:// stackoverflow.com/a/456918/327011

而DrawUI并填写就必须有两个作为活动可以在每两个方向来创建开始与纵向和横向布局的逻辑。

解决方案
  

我缺少的东西?

是的。你是假设你选择的是有点不容易出错。

由于没有经过破坏,和再造的周期,你必须确保你正在处理不断变化的每次的资源的每次的可能的配置变化。

  

不要让活动进行轮换破坏

除非你使用安卓screenOrientation 来强迫你的活动变成一个单一的方向(例如,风景),你不仅可以办理转相关的配置更改。您需要处理的所有的配置更改。否则,只要用户将他们的设备变成一个码头,从码头删除它,从设置更改语言,连接或断开键盘,改变了全球字体缩放,等等,你的应用将打破。

这反过来又意味着,每一个配置的改变,您需要:

  • 在更新你的用户界面为你的潜在的新字符串资源
  • 调整或重新装载布局(通过调整,其中包括改变任何可绘制动画,菜单等。)
  • 在其他任何连接到你的资源(例如,在数组列表的 preferenceFragment

问题是,你要忘记的事。例如,你会错过变化与动作栏项目相关的字符串,所以现在大部分的用户界面是在西班牙和动作栏项目的是英文。事情你会忘记将不太明显(你经常测试你的西班牙语翻译?)。

的种类

I have used both approaches:

  1. Let the activity be destroyed on rotation
  2. Don't let the activity be destroyed on rotation

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.

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

Am I missing something?

UPDATE:

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

When destroying

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

When not destroyed:

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

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

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

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:

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

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天全站免登陆