Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, after some experimentation, it looks like updating the UI widget inside <code>onRestoreInstanceState()</code> is not the way to go, because it always seems to be <code>null</code> at that point. I don't know why they suggest it. Perhaps you have to do it if subclassing Preference, but there are different rules to follow when subclassing DialogPreference...? That would at least explain why ListPreference and EditTextPreference don't do it, because they subclass DialogPreference.</p> <p>In fact, from what I've found, the UI widget does not need to be updated at all! It should have its own save/restore methods that handle its state management for you. For example, here is an excerpt of a DialogPreference subclass I made with a SeekBar widget in it:</p> <pre><code>@Override protected Parcelable onSaveInstanceState() { final Parcelable superState = super.onSaveInstanceState(); final SavedState myState = new SavedState(superState); myState.maxValue = getMaxValue(); //&lt;---- saves mMaxValue myState.value = getValue(); //&lt;---- 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; setMaxValue(myState.maxValue); //&lt;---- updates mMaxValue setValue(myState.value); //&lt;---- updates mValue super.onRestoreInstanceState(myState.getSuperState()); } </code></pre> <p>As you can see, I never update a SeekBar widget anywhere. The SeekBar will save/restore its state all by itself!</p> <p>You'll also notice there are some slight deviations from what is suggested in the Android developer docs. I don't check if the DialogPreference is persistent before saving state, because then the <code>mValue</code> and <code>mMaxValue</code> properties would not get saved if it is. I also call <code>super.onRestoreInstanceState()</code> right at the end, as I found that it never works when it is called earlier.</p> <p>These are just my findings so far. I'm not sure what the right way is, but what I have above seems to work.</p> <p>UPDATE: @whatyouhide wants to know what the <code>setValue</code> and <code>setMaxValue</code> methods look like in my DialogPreference subclass. Here they are:</p> <pre><code>public void setValue(int value) { value = Math.max(Math.min(value, mMaxValue), mMinValue); if (value != mValue) { mValue = value; persistInt(value); notifyChanged(); } } public void setMaxValue(int maxValue) { mMaxValue = maxValue; setValue(Math.min(mValue, mMaxValue)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload