问题
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