为什么不总是使用 android:configChanges="keyboardHidden|orientation" ?
我想知道为什么不在每个(几乎每个;))活动中使用 android:configChanges="keyboardHidden|orientation"
?
优点:
- 无需担心您的活动被旋转,
- 速度更快
不太好:
- 如果布局取决于屏幕尺寸,则需要更改布局(例如具有两列左右的布局)
坏:
- 没有灵活的方式在不同方向上拥有不同的布局
- 使用片段时不太好
但是如果我们不使用不同的布局,为什么不呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
快速背景
默认情况下,当 Android 上发生某些关键配置更改(常见的例子是方向更改)时,Android 会完全重新启动正在运行的 Activity 以帮助其适应此类更改。
当您在 AndroidManifest 中定义
android:configChanges="keyboardHidden|orientation"
时,您就是在告诉 Android:“请不要在拔出键盘或旋转手机时执行默认重置;我想自己处理这件事,是的,我知道我在做什么”这是一件好事吗?我们很快就会看到...
不担心吗?
您开始的优点之一是:
在许多情况下,人们错误地认为,当他们因方向更改(“旋转”)而产生错误时,他们可以通过输入 android:configChanges 来简单地修复它=“键盘隐藏|方向”。
然而, android:configChanges="keyboardHidden|orientation" 无非是一个创可贴。事实上,触发配置更改的方式有很多种。例如,如果用户选择一种新语言(即区域设置已更改),您的 Activity 将以与方向更改相同的方式重新启动。如果您愿意,可以查看所有不同类型配置的列表更改。
编辑:更重要的是,正如 hackbod 在评论中指出的那样,您的活动也会当您的应用程序在后台运行并且 Android 决定通过杀死它来释放一些内存时重新启动。当用户返回您的应用程序时,Android 将尝试以与发生其他配置更改时相同的方式重新启动 Activity。如果你不能处理这个问题 - 用户不会高兴......
换句话说,使用
android:configChanges="keyboardHidden|orientation"
并不能解决你的“担忧”。正确的方法是对您的活动进行编码,以便他们对 Android 抛出的任何重启感到满意。这是一个很好的做法,可以帮助您继续前进,所以要习惯它。那么我应该什么时候使用它呢?
正如您提到的,有一个明显的优势。通过自己处理来覆盖轮换的默认配置更改会加快速度。然而,这种速度确实是以便利为代价的。
简而言之,如果您对纵向和横向使用相同的布局,则通过覆盖可以保持良好的状态。视图将简单地移动以填充剩余空间,而不是完全重新加载活动。
但是,如果由于某种原因您在设备横向时使用不同的布局,则 Android 重新加载您的 Activity 是件好事,因为它将加载正确的布局。 [如果您在这样的 Activity 上使用覆盖,并且想要在运行时进行一些神奇的重新布局...好吧,祝你好运 - 这远非简单]
快速摘要
无论如何,如果
android:configChanges=" KeyboardHidden|orientation"
适合您,然后使用它。但是请一定要测试当某些内容发生变化时会发生什么,因为方向变化并不是触发完整 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.
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...
No worries?
One of the pros you start with is that there is:
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 Activity restart can be triggered.从我的角度来看:如果横向和纵向模式下的布局相同 - 您也可以在应用程序中禁用这两种模式之一。
我之所以这么说,是因为我作为用户希望当我改变方向时该应用程序能为我提供一些好处。如果我如何握手机并不重要,那么我就不需要选择。
以具有 ListView 的应用程序为例,单击 ListItem 后,您希望显示该项目的详细视图。在横向模式下,您可以通过将屏幕一分为二来实现这一点,将 ListView 放在左侧,将详细视图放在右侧。在纵向中,您会将列表显示在一个屏幕中,然后在选择列表项时将屏幕更改为详细视图。在这种情况下,方向改变以及不同的布局都是有意义的。
From my point of view: If the layout is the same in both landscape and portrait mode - you might aswell disable one of the two in your app.
The reason why I state this is that I as a user expect the app to provide me with some benefit, when I change orientation. If it doesn't matter how I hold my phone, then I don't need the choice.
Take for instance an app where you have a ListView, and upon clicking a ListItem you want to be shown a detailed view for that item. In landscape you would od this by dividing the screen in two, having the ListView on the left and the detailed view on the right. In Portrait you would have the list in one screen and then change the screen to the detailed view when a ListItem is selected. In that case orientation change makes sense as well as different layouts.
我不明白为什么......在我看来,偶尔重新启动是可以的......configChanges 可以为我处理大多数情况......也许在某些类型的应用程序中这可能是问题,但这实际上取决于应用程序的类型以及恢复方式应用程序重新启动时的状态...当我的一个应用程序重新启动时,用户会重新登录,并且最后一个活动由我的代码打开,并且用户只是丢失了一些步骤以返回到他所在的位置,但这没什么大不了的。在其他情况下,某些状态总是持续存在并且某些状态总是在重新启动时恢复。当活动重新启动时,它必须是应用程序尚未使用或其他...所以根本没有问题...例如在游戏中这可能是问题,或者在我不知道的其他类型的应用程序中...
我想说的是,当你这样做时,应用程序在正常情况下就能正常工作。而且代码的可读性要高得多,不需要大量的逻辑来保存和恢复,你只能制造新的错误并且必须一直维护它......当然,如果android断电并杀死你的应用程序窗口,它就会失去上下文并再次开始,但这种情况只发生在特殊情况下,在较新的设备上,我相信这种情况越来越罕见......
所以杀了我,但我在应用程序中非常成功地使用了它......
android:configChanges="locale|keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
但我知道对于某些特殊类型的应用程序来说,这可能不是一个好方法,但大多数应用程序都可以接受这一点。
I don see why.... occasional restarts are ok in my opinion... configChanges handles most cases for me... well maybe in some types of applications this can be problem but it depends really on type of app and how you restore state when app restarts... When one of my app restarts user is logged back and last activity opens by my code and user jus loses some steps to go back where he was but not big deal.. In other some state is always persisted and some state is always restored on restart. When activity restarted it had to be that app have not been used or something... so no problem at all... In game for example this can be problem maybe or in some other type of app I don't know...
I say that when you do it this way applications just works fine under normal circumstances. And code is much more readable without ton of logic needed for saving and restoring where u just can make new bugs and have to maintain it all the time... sure if android gets out of power and kill you application window it lose the context and starts again, but this happen just in special situations and on newer devices I belive this is more and more rare...
So kill me, but I use this across applications quite successfully...
android:configChanges="locale|keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
But I understand that for some special kind of applications it may be not good way but most of apps can live with this just OK.
是的,我认为暂停会比释放播放器更快。不过还是有停顿。
现在已经找到了不会暂停歌曲的解决方案。
在清单中声明您将处理屏幕方向的配置更改,然后使用 onConfigurationChanged 方法加载布局文件。通过在 logCat 中执行此操作,我可以看到 onPause、onCreate 和 onPause。 onResume 不会被调用,因此歌曲不会暂停。
更新清单以处理方向。
添加此代码
<前><代码>@Override
公共无效onConfigurationChanged(配置newConfig){
// TODO 自动生成的方法存根
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
}
Yeah I think pausing will make it quicker than releasing the player. Still have the pause though.
Have now found a solution that won't pause the song.
State in the manifest that you will handle the config change for screen orientation and then use the onConfigurationChanged method to load the layout file. By doing this in logCat I can see onPause, onCreate & onResume aren't called, and therefore the song isn't paused.
update the manifest to handle the orientation.
add this code