Screen auto rotates when unlocking
<p>Hi I am trying to save some values in my activity but I'm having a problem when I save it with  onSaveInstanceState and restore with onRestoreInstanceState, I am saving an int years and it saves the first time the screen changes orientation, the spinner repopulates and sets it at the last value , but not when I switch back the orientation again.</p>

<p>My orientation is set to landscape in my manifest, and I have the two save and restore method overridden but it doesn't seem to work, do I need to do something in onPause aswell?</p>

<p>here is my code:</p>

<pre><code>    int years;//global variable
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Spinner spinnerYears = (Spinner) findViewById(R.id.spinYears);//Spinner
            final ArrayAdapter &lt;Integer&gt; adapter = new ArrayAdapter &lt;Integer&gt;(this,android.R.layout.simple_spinner_item );
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        for(int i=0;i&lt;=100;i++)
        {
           adapter.add(i);
        }
        spinnerYears.setAdapter(adapter);
        years = spinnerYears.getSelectedItemPosition();

   }//onCreate

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

  savedInstanceState.putInt("MyInt", years);


}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  Spinner spinnerYears = (Spinner) findViewById(R.id.spinYears);//Spinner
    final ArrayAdapter &lt;Integer&gt; adapter = new ArrayAdapter &lt;Integer&gt; (this,      android.R.layout.simple_spinner_item );
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        for(int i=0;i&lt;=100;i++)
     {
        adapter.add(i);
     }

      spinnerYears.setAdapter(adapter);
      TextView tvYears = (TextView) findViewById(R.id.tvYears);//spinner Tv
  tvYears.setVisibility(TextView.GONE);


 int myInt = savedInstanceState.getInt("MyInt");
  spinnerYears.setSelection(myInt);//
  Toast.makeText(getBaseContext(), "Restored int"+myInt, Toast.LENGTH_LONG).show();
}
</code></pre>
