为什么不总是使用 android:configChanges="keyboardHidden|orientation"? [英] Why not use always android:configChanges="keyboardHidden|orientation"?

查看:27
本文介绍了为什么不总是使用 android:configChanges="keyboardHidden|orientation"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么不在每个(几乎每个 ;))活动中使用 android:configChanges="keyboardHidden|orientation" ?

I was wondering why not use android:configChanges="keyboardHidden|orientation" in every (almost every ;)) activity?

商品:

  • 无需担心您的活动被轮换
  • 速度更快

不太好:

  • 如果布局取决于屏幕尺寸(例如两列左右的布局),则需要更改布局

不好:

  • 没有灵活的方式在不同方向上有不同的布局
  • 使用片段时不太好

但如果我们不使用不同的布局,为什么不呢?

But if we don't use different layouts, why not?

推荐答案

快速背景

默认情况下,当 Android 上发生某些关键配置更改时(一个常见的例子是方向更改),Android 会完全重新启动正在运行的 Activity 以帮助其适应此类更改.

Quick Background

By default, when certain key configuration changes happen on Android (a common example is an orientation change), Android fully restarts the running Activity to help it adjust to such changes.

当您在 AndroidManifest 中定义 android:configChanges="keyboardHidden|orientation" 时,您是在告诉 Android:请不要在拔出键盘或手机时进行默认重置旋转了;我想自己处理这个问题.是的,我知道我在做什么"

When you define android:configChanges="keyboardHidden|orientation" in your AndroidManifest, you are telling Android: "Please don't do the default reset when the keyboard is pulled out, or the phone is rotated; I want to handle this myself. Yes, I know what I'm doing"

这是好事吗?我们很快就会看到...

Is this a good thing? We shall soon see...

您开始的其中一个优点是:

One of the pros you start with is that there is:

无需担心您的活动被轮换

no need to worry about your activity been rotated

在许多情况下,人们错误地认为当他们有一个由方向改变(旋转")产生的错误时,他们可以通过输入 android:configChanges="keyboardHidden|orientation" 来简单地修复它.

In many cases, people mistakenly believe that when they have an error that is being generated by an orientation change ("rotation"), they can simply fix it by putting in android:configChanges="keyboardHidden|orientation".

然而,android:configChanges="keyboardHidden|orientation" 只不过是一个创可贴.事实上,可以通过多种方式触发配置更改.例如,如果用户选择了一种新语言(即区域设置已更改),您的 Activity 将按照与方向更改相同的方式重新启动.如果您愿意,您可以查看 所有不同类型配置的列表变化.

However, android:configChanges="keyboardHidden|orientation" is nothing more than a bandaid. In truth, there are many ways a configuration change can be triggered. For example, if the user selects a new language (i.e. the locale has changed), your activity will be restarted in the same way it does by an orientation change. If you want you can view a list of all the different types of config changes.

编辑:不过,更重要的是,正如 hackbod 在评论中指出的那样,您的当您的应用程序处于后台并且 Android 决定通过杀死它来释放一些内存时,Activity 也会重新启动.当用户返回您的应用程序时,Android 将尝试以与其他配置更改相同的方式重新启动 Activity.如果你不能处理这个 - 用户会不高兴......

Edit: More importantly, though, as hackbod points out in the comments, your activity will also be restarted when your app is in the background and Android decides to free up some memory by killing it. When the user comes back to your app, Android will attempt to restart the activity in the same way it does if there was some other configuration change. If you can't handle that - the user will not be happy...

换句话说,使用 android:configChanges="keyboardHidden|orientation" 并不能解决您的后顾之忧".正确的方法是对您的活动进行编码,以便他们对 Android 向他们抛出的任何重启感到满意.这是一个很好的做法,可以帮助您走上正轨,所以要习惯它.

In other words, using android:configChanges="keyboardHidden|orientation" is not a solution for your "worries." The right way is to code your activities so that they are happy with any restart Android throws at them. This is a good practice that will help you down the road, so get used to it.

正如您提到的,有一个明显的优势.通过自己处理来覆盖轮换的默认配置更改将加快速度.然而,这种速度确实以便利为代价.

As you mentioned there is a distinct advantage. Overwriting the default configuration change for a rotation by handling it yourself will speed things up. However, this speed does come with a price of convenience.

简而言之,如果您对纵向和横向使用相同的布局,那么通过进行覆盖,您就处于良好状态.视图将简单地移动以填充剩余空间,而不是完全重新加载 Activity.

To put it simply, if you use the same layout for both portrait and landscape you're in good shape by doing the overwrite. Instead of a full-blown reload of the activity, the views will simply shift around to fill the remaining space.

然而,如果由于某种原因您在设备处于横向时使用不同的布局,Android 重新加载您的 Activity 的事实很好,因为它随后会加载正确的布局.[如果你在这样的 Activity 上使用覆盖,并想在运行时做一些神奇的重新布局......好吧,祝你好运 - 这远不简单]

However, if for some reason you use a different layout when the device is in landscape, the fact that Android reloads your Activity is good because it will then load up the correct layout. [If you use the override on such an Activity, and want to do some magical re-layout at runtime... well, good luck - it's far from simple]

无论如何,如果 android:configChanges="keyboardHidden|orientation" 适合您,请使用它.但是一定要测试当事情发生变化时会发生什么,因为方向改变并不是触发完整活动重启的唯一方式.

By all means, if android:configChanges="keyboardHidden|orientation" is right for you, then use it. But PLEASE be sure to test what happens when something changes, because an orientation change is not the only way a full Activity restart can be triggered.

这篇关于为什么不总是使用 android:configChanges="keyboardHidden|orientation"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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