2

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"
Thijs Limmen
  • 624
  • 7
  • 9
  • Please, post the logcat – Nickolai Astashonok Apr 18 '13 at 15:15
  • No particular Message or error showing in LogCat. It might just be natural behaviour? Basically the Nested ScreenPreference closes and the main ScreenPreference appears again. I want the focus to stick to the Nested ScreenPreference Obviously. – Thijs Limmen Apr 18 '13 at 15:30
  • I have a nested fragments in Preferences in my application. It works fine with location changes... So if your fragment closes, you have to get smth in logcat... Otherwise, I can't help you – Nickolai Astashonok Apr 19 '13 at 05:11
  • I'm having the same issue. Did you ever find a resolution? – Ifrit Aug 20 '13 at 18:39
  • @JaySoyer Look at the solution I put up. Not sure if this fixed it. It is fixed in my app. – Thijs Limmen Aug 22 '13 at 11:30
  • Are you saving and restoring your instances states correctly. When a screen rotation occurs, the activity is closed and re-created. You have to memorize the state of your nested preferences and re-created that state. You will not get any error. Simply the state are not re-created and you find yourself in the same state as when the activity is first created. – HpTerm Aug 22 '13 at 12:24
  • Perhaps you should post the code of the activity and fragment used. – HpTerm Aug 22 '13 at 12:26
  • When you use android:configChanges you ask the activity to not be destroy an re-created that's why it is working. I am not sure but I think it is not a good practice. A correct way of doing is saving and restoring the states correctly. – HpTerm Aug 22 '13 at 12:28

1 Answers1

5

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.

Community
  • 1
  • 1
Ifrit
  • 6,791
  • 8
  • 50
  • 79