I am implementing my own custom DialogPreference subclass that has a SeekBar used for persisting an integer. I'm a little confused about what needs to go into onSaveInstanceState()
and onRestoreInstanceState()
. Specifically, do you need to update the UI widget that the user interacts with (in my case, the SeekBar widget) in onRestoreInstanceState()
?
The reason I am confused is that the API doc article here tells you to do this:
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
return superState;
}
final SavedState myState = new SavedState(superState);
myState.value = mNewValue; //<------------ saves mNewValue
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
mNumberPicker.setValue(myState.value); //<------------ updates the UI widget, not mNewValue!
}
But looking at the source for some official Android Preference classes (EditTextPreference and ListPreference), the UI widget is not updated in onRestoreInstanceState()
. Only the underlying value of the Preference is (in the example above, that would be mNewValue
).
Here is the relevant source for EditTextPreference:
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
return superState;
}
final SavedState myState = new SavedState(superState);
myState.value = getValue(); //<---- saves mValue
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
setValue(myState.value); //<---- updates mValue, NOT the UI widget!
}
So, what's the consensus? Where I am supposed to update the UI widget (if I am supposed to update it at all...)?
setValue
andsetMaxValue
methods you defined in your customDialogPreference
? If so could you post the code for those methods? – Druggist