问题描述
我最近偶然发现一个问题.
I recently stumbled upon a problem.
我正在像这样使用嵌套的首选项屏幕:
I am working with a Nested PreferenceScreen like this:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceScreen
android:key="pref_name"
android:title="@string/pref_title" >
</PreferenceScreen>
当我的屏幕将焦点放在嵌套首选项"屏幕上并且更改了屏幕方向时,嵌套首选项屏幕"将关闭.
When my screen has the focus on the Nested Preference Screen and I change screenorientation, the Nested PreferenceScreen closes.
我也尝试过包括:
android:configChanges="orientation|keyboardHidden"
在AndroidManifest.xml中,但无效.
in AndroidManifest.xml, but didn't work.
有人对此有解决方案吗?
Does anyone have a solution for this?
可能的解决方案:
我确实找到了解决方案.我以为是这行:
I did find the solution. I thought it was this line:
android:configChanges="keyboardHidden|orientation|screenSize"
推荐答案
知道了.为了防止嵌套屏幕在旋转时关闭,您需要确保为父屏幕指定了键值.而已.例如:
Got it. In order to prevent a nested screen from closing on rotation, you need to make sure the parent screen is given a key value. Thats it. Eg:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="useless_key">
<PreferenceScreen
android:key="pref_name"
android:title="@string/pref_title" >
</PreferenceScreen>
</PreferenceScreen>
旁注,尽管重写onConfigChanges解决了该问题,但您几乎永远都不要这样做.它完全改变了活动的正常行为.轮换只是配置更改发生的众多原因之一.如果您的活动"无法正确处理轮换,那么在处理其他条件时也会失败.查看此有见地的帖子以获取更多信息.
Side note, though overriding onConfigChanges solved the issue, you should almost never do so. It completely changes how an Activity normally behaves. Rotation is just one of many reasons why a config change happens. If your Activity can't handle a rotation properly, then it'll also fail on handling those other conditions. Check out this insightful post for more.
这篇关于嵌套首选项屏幕在Android中的Screenorientation更改时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!