- PreferenceScreen not being restored on orientation change
- 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);
- }
- public class HelloPreferenceActivity extends PreferenceActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- addPreferencesFromResource(R.xml.preferences);
- }
- }
- <?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>
- 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);
- }
- }
- }
- 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()");
- }
- }
- }
- }