Nested Preference Screen closes on Screenorientation change in Android

て烟熏妆下的殇ゞ 提交于 2019-12-24 03:30:58

问题


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.

I have also tried including:

android:configChanges="orientation|keyboardHidden"

in AndroidManifest.xml, but didn't work.

Does anyone have a solution for this?

EDIT POSSIBLE SOLUTION:

I did find the solution. I thought it was this line:

android:configChanges="keyboardHidden|orientation|screenSize"

回答1:


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>

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.



来源:https://stackoverflow.com/questions/16086487/nested-preference-screen-closes-on-screenorientation-change-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!