PreferenceScreen not being restored on orientation change
<p>I had the following (minor but nagging!) problem: I've got a <code>PreferenceActivity</code> with a XML preference hierarchy definition with "sub <code>PreferenceScreens</code>", i. e. several <code>PreferenceScreens</code> under the top level <code>PreferenceScreen</code>, and when the user clicks them, a sub hierarchy of other preferences is being displayed.</p>

<p>If I have displayed such a <strong>sub</strong> <code>PreferenceScreen</code> and the orientation is being changed from portrait to landscape or vice versa, the <strong>main</strong> <code>PreferenceScreen</code> is displayed afterwards. Which means: The preference hierarchy state was not being restored properly. I would expect the <strong>sub</strong> <code>PreferenceScreen</code> to be displayed after the orientation change, just as before the orientation change.</p>

<p>Now I found out that the problem can be solved by assigning any random key (e. g. "dummy") to the sub <code>PreferenceScreen</code>. Which does not make any sense! Why would a <code>PreferenceScreen</code> ever need a key (except as a workaround for the above problem)?</p>

<p>My actual question: Is this behaviour a bug in the framework class(es)? If not: Can someone explain me how that makes sense?</p>

<p>My example code:</p>

<p>HelloAndroid.java:</p>

<pre><code>public class HelloAndroid extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button view = new Button(this);
        view.setText("Start Preference");
        final Context ctx = this;
        view.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(ctx, HelloPreferenceActivity.class));
            }
        });
        this.setContentView(view);
    }
</code></pre>

<p>HelloPreferenceActivity.java:</p>

<pre><code>public class HelloPreferenceActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}
</code></pre>

<p>preference.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    &lt;CheckBoxPreference android:key="examplePref"
        android:title="ExamplePref"&gt;
    &lt;/CheckBoxPreference&gt;
    &lt;PreferenceScreen android:title="SubScreen..."
        android:key="dummy"&gt; &lt;!-- works with, does not work without this key --&gt;
        &lt;CheckBoxPreference android:key="exPrefSubScreen"
            android:title="ExPrefSubScreen"&gt;
        &lt;/CheckBoxPreference&gt;
    &lt;/PreferenceScreen&gt;
&lt;/PreferenceScreen&gt;
</code></pre>
