Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5981519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:57:19+00:00 2026-05-22T21:57:19+00:00

I had the following (minor but nagging!) problem: I’ve got a PreferenceActivity with a

  • 0

I had the following (minor but nagging!) problem: I’ve got a PreferenceActivity with a XML preference hierarchy definition with “sub PreferenceScreens“, i. e. several PreferenceScreens under the top level PreferenceScreen, and when the user clicks them, a sub hierarchy of other preferences is being displayed.

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

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

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

My example code:

HelloAndroid.java:

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);
    }

HelloPreferenceActivity.java:

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

preference.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference android:key="examplePref"
        android:title="ExamplePref">
    </CheckBoxPreference>
    <PreferenceScreen android:title="SubScreen..."
        android:key="dummy"> <!-- works with, does not work without this key -->
        <CheckBoxPreference android:key="exPrefSubScreen"
            android:title="ExPrefSubScreen">
        </CheckBoxPreference>
    </PreferenceScreen>
</PreferenceScreen>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:57:19+00:00Added an answer on May 22, 2026 at 9:57 pm

    This is by design, the state of Preference is associated with its key. If Preference has no key then it will not be able to save/restore its state.

    The similar behavior can be found in views withing layouts. If view has no id specified for it, it will not be able to restore state after configuration change.

    And to back my words up, here is an example of how Preference saves its state:

    void dispatchSaveInstanceState(Bundle container) {
        if (hasKey()) {
            mBaseMethodCalled = false;
            Parcelable state = onSaveInstanceState();
            if (!mBaseMethodCalled) {
                throw new IllegalStateException(
                        "Derived class did not call super.onSaveInstanceState()");
            }
            if (state != null) {
                container.putParcelable(mKey, state);
            }
        }
    }
    

    And here is how Preference tries to restore its state:

    void dispatchRestoreInstanceState(Bundle container) {
        if (hasKey()) {
            Parcelable state = container.getParcelable(mKey);
            if (state != null) {
                mBaseMethodCalled = false;
                onRestoreInstanceState(state);
                if (!mBaseMethodCalled) {
                    throw new IllegalStateException(
                            "Derived class did not call super.onRestoreInstanceState()");
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.