Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 13th, 2012  |  syntax: None  |  size: 2.20 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PreferenceScreen not being restored on orientation change
  2. public class HelloAndroid extends Activity {
  3.     @Override
  4.     public void onCreate(Bundle savedInstanceState) {
  5.         super.onCreate(savedInstanceState);
  6.  
  7.         Button view = new Button(this);
  8.         view.setText("Start Preference");
  9.         final Context ctx = this;
  10.         view.setOnClickListener(new View.OnClickListener() {
  11.             public void onClick(View v) {
  12.                 startActivity(new Intent(ctx, HelloPreferenceActivity.class));
  13.             }
  14.         });
  15.         this.setContentView(view);
  16.     }
  17.        
  18. public class HelloPreferenceActivity extends PreferenceActivity {
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         addPreferencesFromResource(R.xml.preferences);
  23.     }
  24. }
  25.        
  26. <?xml version="1.0" encoding="utf-8"?>
  27. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  28.     <CheckBoxPreference android:key="examplePref"
  29.         android:title="ExamplePref">
  30.     </CheckBoxPreference>
  31.     <PreferenceScreen android:title="SubScreen..."
  32.         android:key="dummy"> <!-- works with, does not work without this key -->
  33.         <CheckBoxPreference android:key="exPrefSubScreen"
  34.             android:title="ExPrefSubScreen">
  35.         </CheckBoxPreference>
  36.     </PreferenceScreen>
  37. </PreferenceScreen>
  38.        
  39. void dispatchSaveInstanceState(Bundle container) {
  40.     if (hasKey()) {
  41.         mBaseMethodCalled = false;
  42.         Parcelable state = onSaveInstanceState();
  43.         if (!mBaseMethodCalled) {
  44.             throw new IllegalStateException(
  45.                     "Derived class did not call super.onSaveInstanceState()");
  46.         }
  47.         if (state != null) {
  48.             container.putParcelable(mKey, state);
  49.         }
  50.     }
  51. }
  52.        
  53. void dispatchRestoreInstanceState(Bundle container) {
  54.     if (hasKey()) {
  55.         Parcelable state = container.getParcelable(mKey);
  56.         if (state != null) {
  57.             mBaseMethodCalled = false;
  58.             onRestoreInstanceState(state);
  59.             if (!mBaseMethodCalled) {
  60.                 throw new IllegalStateException(
  61.                         "Derived class did not call super.onRestoreInstanceState()");
  62.             }
  63.         }
  64.     }
  65. }