为什么不使用总是安卓configChanges =" keyboardHidden |取向和QUOT;? [英] Why not use always android:configChanges="keyboardHidden|orientation"?

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

问题描述

我在想,为什么不使用安卓configChanges = |在每一个keyboardHidden方向(几乎所有))的活动?

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(一个常见的​​例子是一个方向改变),机器人完全重新启动运行的活动,以帮助它适应这样的变化。

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.

当你定义安卓configChanges =keyboardHidden |定位在AndroidManifest,你告诉机器人:请不要做默认的复位时,键盘拔出,或将手机旋转。我想处理这个自己是的,我知道我在做什么

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 |定位

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

不过,安卓configChanges =keyboardHidden |定位无非是一个绷带。事实上,有很多方法的配置变化可以触发。例如,如果用户选择了一个新的语言(即区域设置已经改变),您的活动将被重新启动在它由一个方向变化的方式相同。如果你愿意,你可以查看各种不同类型的配置更改名单

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决定杀死它释放一些内存。当用户回来到您的应用程序,Android将尝试重新启动活动确实是有一些其他的配置改变以同样的方式。如果你不能处理 - 用户会不高兴......

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

在换句话说,使用安卓configChanges =keyboardHidden |定位不是你的解决方案的担忧。正确的做法是,以$ C C你的活动$使他们很高兴与任何重启的Andr​​oid在他们抛出。这是一个很好的做法,这将有助于你在路上,所以也就习惯了。

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.

要简单地说,如果你使用相同的布局纵向和横向你是在良好的状态通过执行覆盖。取而代之的是活动的全面的重新加载,该意见将只是转移周围,以填补剩余空间。

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的重新加载你的活动是好事,因为这将加载了正确的布局的事实。 [如果您使用此类活动的覆盖,并希望做一些神奇的重新布局在运行时......好,好运气 - 这是远非如此简单]

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]

通过一切手段,如果安卓configChanges =keyboardHidden |定位是适合你的,然后使用它。但是,一定要考试的时候有新的变化会发生什么,因为一个方向的变化是不是一个完整的活性的研究重新启动就可以被触发的唯一途径。

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 Acitivity restart can be triggered.

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

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