title: Custom inline SeekBarPreference - how to set SeekBar progress on the 1st run?
body: <p>I have prepared <a href="https://github.com/afarber/android-newbie/tree/q19/MyPrefs" rel="noreferrer">a simple test project</a> for my question at GitHub.</p>
  
  <p>In my project there is a custom <em>inline</em> <a href="https://github.com/afarber/android-newbie/blob/q19/MyPrefs/src/de/afarber/myprefs/SeekBarPreference.java" rel="noreferrer">SeekBarPreference</a>, which mostly works fine (its summary is updated when seekbar is being dragged and it saves integer value):</p>
  
  <p><img src="https://i.stack.imgur.com/U7IdR.png" alt="app screenshot"></p>
  
  <p>However there is a problem: </p>
  
  <p>On the very 1st run of the app (you might need to <strong>uninstall</strong> my app when you try see the error again) the progress of the <code>SeekBar</code> is not set (but the summaries are set):</p>
  
  <p><img src="https://i.stack.imgur.com/EQ984.png" alt="app screenshot"></p>
  
  <p>My question is: how to fix this issue in my code?</p>
  
  <p>I have tried adding mSeekBar.setProgress(mProgress) in different spots of <a href="https://github.com/afarber/android-newbie/blob/q19/MyPrefs/src/de/afarber/myprefs/SeekBarPreference.java" rel="noreferrer">SeekBarPreference.java</a>, but just can't find the correct place for that code.</p>
  
  <p>Below are excerpts from my source code (in case Stackoverflow ever outlives GitHub) -</p>
  
  <p><a href="https://github.com/afarber/android-newbie/blob/q19/MyPrefs/src/de/afarber/myprefs/MainActivity.java" rel="noreferrer">MainActivity.java</a>:</p>
  
  <pre><code>PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
  
  getFragmentManager().beginTransaction()
      .addToBackStack(null)
      .replace(R.id.root, new PrefFragment(), "prefs")
      .commit();
  </code></pre>
  
  <p><a href="https://github.com/afarber/android-newbie/blob/q19/MyPrefs/src/de/afarber/myprefs/PrefFragment.java" rel="noreferrer">PrefFragment.java</a>:</p>
  
  <pre><code>@Override
  public void onResume() {
      super.onResume();
  
      SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
      // set the summaries from saved values
      onSharedPreferenceChanged(prefs, SEEK_1);
      onSharedPreferenceChanged(prefs, SEEK_2);
      prefs.registerOnSharedPreferenceChangeListener(this);
  }
  
  @Override
  public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
  
      if (SEEK_1.equals(key)) {
          int i1 = prefs.getInt(key, DEFAULT_1);
          mSeek1.setSummary("$ " + i1);
      } else if (SEEK_2.equals(key)) {
          int i2 = prefs.getInt(key, DEFAULT_2);
          mSeek2.setSummary("$ " + i2);
      }       
  }  
  </code></pre>
  
  <p><a href="https://github.com/afarber/android-newbie/blob/q19/MyPrefs/src/de/afarber/myprefs/SeekBarPreference.java" rel="noreferrer">SeekBarPreference.java</a> (the complete source code):</p>
  
  <pre><code>public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {
  
      private SeekBar mSeekBar; // FIXME how to set its progress?
      private int mProgress;
  
      public SeekBarPreference(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      @Override
      protected View onCreateView(ViewGroup parent) {
          LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View view = inflater.inflate(R.layout.preference_seekbar, parent, false);
          // can also be done in onBindView, does not work either
          mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
          mSeekBar.setProgress(mProgress);
          mSeekBar.setOnSeekBarChangeListener(this);
          return view;
      }
  
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
          if (!fromUser)
              return;
  
          setValue(progress);
      }
  
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
          // not used
      }
  
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
          // not used
      }
  
      @Override
      protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
          setValue(restoreValue ? getPersistedInt(mProgress) : (Integer) defaultValue);
      }
  
      public void setValue(int value) {
          if (shouldPersist()) {
              persistInt(value);
          }
  
          if (value != mProgress) {
              mProgress = value;
              notifyChanged();
          }
      }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>What I have unsuccessfully tried sofar -</p>
  
  <p>1) Adding the following lines to the <code>onCreateView()</code> method:</p>
  
  <pre><code>    int progress = getPersistedInt(mProgress); // shows 0 in debugger
      mSeekBar.setProgress(progress);
  </code></pre>
  
  <p>2) Adding the following lines to the <code>setValue()</code> method:</p>
  
  <pre><code>    if (value != mProgress) {
          mProgress = value;
          mSeekBar.setProgress(mProgress);
          notifyChanged();
      }
  </code></pre>
  
  <p>Alas this does not work, the progress of the <code>mSeekBar</code> stays at 0.</p>
  
  <p>Also, there is a <a href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/preference/SeekBarPreference.java" rel="noreferrer">SeekBarPreference by Google</a> - but I don't understand how it works there (or if it works at all).</p>