什么时候使用 android:configChanges="keyboardHidden|orientation"

以下转自StackOverFlow的回答:

Is this a good thing? We shall soon see…

No worries?
One of the pros you start with is that there is:

no need to worry about your activity been rotated
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”.

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.

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…

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.

So when should I use 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.

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]

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

总结:当你横向和纵向的布局相同的时候时,可以使用,但是如果你使用的是多布局也就是横竖屏使用不同的布局时就不能使用。同是当你进行这种配置时,请进行各种测试,因为触发Activity Restart的原因有很多,例如用户改变设备语言时Activity也会restart。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值