How to retain a value overwritten by the instance state restoring mechanism?
<p>In my application, I have a <code>Spinner</code> whose <code>onItemSelected</code> initializes the value of a class member, say, <code>a</code>, to its default value of <code>0</code>. The user may then perform steps to change the value of <code>a</code>. I am saving the value of <code>a</code> in <code>onSaveInstanceState</code> and restoring it in <code>onRestoreInstanceState</code>. However, apparently as a result of restoring of the <code>Spinner</code>, its <code>onItemSelected</code> gets called and puts <code>a</code> back to zero. I want the value entered by the user to be preserved.</p>

<p>Note, however, that the old value of <code>a</code> would not make sense in any other setting of the <code>Spinner</code> (hence zeroing it upon item selection). Unfortunately, it is also hard to validate it. So, not knowing that the call to <code>onItemSelected</code> was fake, putting <code>a</code> to zero is inevitable.</p>

<p>The problem is that the input event handler seems to be put into some queue and called <em>after</em> <code>onCreate</code> / <code>onRestoreInstanceState</code> is finished (twice, which is another thing I don't understand). My question thus is, at what moment should I restore <code>a</code> to the non-default value from the saved instance state? Should I put this task at the end of the input event queue somehow? Or is there a way of distinguishing whether the call of <code>onItemSelected</code> happened as a result of an actual user input or just instance state restoration?</p>

<p>Code example:</p>

<pre><code>private String KEY = "(...).A";
private int a;

public void onItemSelected(AdapterView&lt;?&gt; parentView, View childView, int position, long id) {
  /* other important stuff */
  a = 0;
  Log.d("info", "a zeroed");
}

public void onSaveInstanceState(Bundle state) {
  super.onSaveInstanceState(state);
  state.putInt(KEY, a);
}

public void onRestoreInstanceState(Bundle state) {
  super.onRestoreInstanceState(state);
  a = state.getInt(KEY, a);
  Log.d("info", "a restored");
}
</code></pre>

<p>After a change of orientation, the log says</p>

<pre><code>info  a restored
info  a zeroed
info  a zeroed
</code></pre>
