title: Using a NumberPicker as a DialogPreference. Why won't my setting save?
body: <p>just learning android code here. I'm not sure what I'm missing, but I'm trying to create a custom <code>DialogPreference</code> that uses a <code>NumberPicker</code> which will let the user choose the year. I've tried to follow android's <a href="http://developer.android.com/guide/topics/ui/settings.html#Custom" rel="nofollow">Settings Guide</a>, but my chosen value does not save. It saves as long as I don't exit the app, but if I exit then it resets to the default value when I relaunch the app. What am I missing?</p>
  
  <p>SettingsActivity code:</p>
  
  <pre><code>public class SettingsActivity extends PreferenceActivity {
  
      @Override
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
  
        }
  
      public static class MyPreferenceFragment extends PreferenceFragment
      {
          @Override
          public void onCreate(final Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              addPreferencesFromResource(R.xml.app_preferences);
          }
      }
  }
  </code></pre>
  
  <p>app_preferences.xml (layout for SettingsActivity)</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;EditTextPreference /&gt;
  
      &lt;com.NsouthProductions.gradetrackerpro.SemesterPickerPreference
          android:key="MY_PICKER"
          android:title="@string/year_pref_str"
          android:summary="Choose a year here"
          android:defaultValue="2005"
      /&gt; 
  
  &lt;/PreferenceScreen&gt;
  </code></pre>
  
  <p>SemesterPickerPreference (extension of DialogPreference)</p>
  
  <pre><code>public class SemesterPickerPreference extends DialogPreference {
  
      private int year;
      private NumberPicker semPick;
      private int DEFAULT_VALUE;
  
      public SemesterPickerPreference(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          DEFAULT_VALUE = 2003;
  
          setDialogLayoutResource(R.layout.semester_picker_dialog);
          setPositiveButtonText(android.R.string.ok);
          setNegativeButtonText(android.R.string.cancel);
  
          setDialogIcon(null);
      }
  
      @Override
      protected void onDialogClosed(boolean positiveResult) {
  
          if (positiveResult) {
              persistInt(semPick.getValue());
          }
      }
  
      @Override
      protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
          if (restorePersistedValue) {
              // Restore existing state
              year = this.getPersistedInt(DEFAULT_VALUE);
          } else {
              // Set default state from the XML attribute
              year = (Integer) defaultValue;
              persistInt(year);
          }
      }
  
      @Override
      protected Object onGetDefaultValue(TypedArray a, int index) {
          return a.getInteger(index,  DEFAULT_VALUE);
      }
  
      @Override
      protected View onCreateDialogView() {
  
          int max = 2038;
          int min = 2001;
  
          LayoutInflater inflater =
                  (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View view = inflater.inflate(R.layout.semester_picker_dialog, null);
  
          semPick = (NumberPicker) view.findViewById(R.id.semester_picker);
  
          // Initialize state
          semPick.setMaxValue(max);
          semPick.setMinValue(min);
          semPick.setValue(this.getPersistedInt(DEFAULT_VALUE));
          semPick.setWrapSelectorWheel(false);
  
          return view;
      }
  
  
  
      //  This code copied from android's settings guide.
  
      private static class SavedState extends BaseSavedState {
          // Member that holds the setting's value
          // Change this data type to match the type saved by your Preference
          int value;
  
          public SavedState(Parcelable superState) {
              super(superState);
          }
  
          public SavedState(Parcel source) {
              super(source);
              // Get the current preference's value
              value = source.readInt();  // Change this to read the appropriate data type
          }
  
          @Override
          public void writeToParcel(Parcel dest, int flags) {
              super.writeToParcel(dest, flags);
              // Write the preference's value
              dest.writeInt(value);  // Change this to write the appropriate data type
          }
  
          // Standard creator object using an instance of this class
          public static final Parcelable.Creator&lt;SavedState&gt; CREATOR =
                  new Parcelable.Creator&lt;SavedState&gt;() {
  
              public SavedState createFromParcel(Parcel in) {
                  return new SavedState(in);
              }
  
              public SavedState[] newArray(int size) {
                  return new SavedState[size];
              }
          };
      }
  
  
  
      @Override
      protected Parcelable onSaveInstanceState() {
          final Parcelable superState = super.onSaveInstanceState();
          // Check whether this Preference is persistent (continually saved)
          if (isPersistent()) {
              // No need to save instance state since it's persistent, use superclass state
              return superState;
          }
  
          // Create instance of custom BaseSavedState
          final SavedState myState = new SavedState(superState);
          // Set the state's value with the class member that holds current setting value
          myState.value = year;
          return myState;
      }
  
      @Override
      protected void onRestoreInstanceState(Parcelable state) {
          // Check whether we saved the state in onSaveInstanceState
          if (state == null || !state.getClass().equals(SavedState.class)) {
              // Didn't save the state, so call superclass
              super.onRestoreInstanceState(state);
              return;
          }
  
          // Cast state to custom BaseSavedState and pass to superclass
          SavedState myState = (SavedState) state;
          super.onRestoreInstanceState(myState.getSuperState());
  
          // Set this Preference's widget to reflect the restored state
          semPick.setValue(myState.value);
      }
  }
  </code></pre>
  
  <p>semester_picker_dialog.xml (layout for my semester/year picker)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:orientation="horizontal" 
      android:gravity="center_horizontal" &gt;
  
      &lt;NumberPicker
          android:id="@+id/semester_picker"
          android:layout_centerInParent="true"
          android:layout_width="100dip"
          android:layout_height="match_parent" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>