title: How to use saved instance state and restore instance state with spinners in in Android?
body: <p>I have a spinner in my activity with a list of options,</p>
  
  <p>When the user selects an option, I have used OnItemSelected adapter to get the position value of the selected option and within that i have added some If Else Statements. The If statement, actually generates a random string from the predefined database and displays on TextView.</p>
  
  <p>Now i will be switching back to another activity and again come back to this activity. Now the problem is by default the first option of the spinner is only selected, I need to update with the last selected option of the user and when the activity starts again that option's functions must be run.</p>
  
  <p>I did some searches too and found that this can be done by using <code>onSaveInstanceState</code> and <code>onRestoreInstanceState</code> now :</p>
  
  <ul>
  <li>How do I use these two and where should i put them, inside the onItemSelected(adapter) or Outside?</li>
  <li>How do I update(or store) the new position value selected by the user to the random generated code?</li>
  </ul>
  
  <p>For clarity please check my code: </p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layout_scheduledactivity);
  
      /**displayed when the activity is launched (this should be 
      updated with the user selected option)**/
      Resources res = getResources();
      myString = res.getStringArray(R.array.normal); 
      String q = myString[rgenerator.nextInt(myString.length)];
      TextView tv = (TextView) findViewById(R.id.text1);
      tv.setText(q);
  
      //initiating the spinner
        Spinner s = (Spinner) findViewById( R.id.spinner);
        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
              @Override
      public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos, long id) {
   if (pos == 1){
  
       //displayed on the time when user selects the option
       Resources res = getResources();
       myString = res.getStringArray(R.array.hardships); 
       String q = myString[rgenerator.nextInt(myString.length)];
       TextView tv = (TextView) findViewById(R.id.text1);
       tv.setText(q);
  
       }
  
  else {
      //do nothing
       }
  </code></pre>
  
  <p><strong>EDITED QUESTION:</strong></p>
  
  <p>I have edited the question for more clarity. Basically what I am trying to do is, I want to save the position selected by they user, with the spinner. When the user clicks "Thankyou" <code>Button</code> the activity will finish and will start after few minutes called by a Broadcast receiver from a different class. When the activity starts again the last selected option (previous activity) of the user must be as default in this activity.</p>
  
  <p>I think if you look at the code the problem is in save and restore instance state.</p>
  
  <p><strong>Please have a look at my code:</strong></p>
  
  <pre><code>import java.util.Random;
  import android.app.Activity;
  import android.content.res.Resources;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.Spinner;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MyScheduledActivity extends Activity {
  
      private String[] myString;
      private static final Random rgenerator = new Random();
      protected int mPos;
    protected String mSelection;
    String situation[] = {"Normal","Hardship","Sadness","Exam"};
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layout_scheduledactivity);
  
  
      if (mPos == 0) {
      Resources res = getResources();
      myString = res.getStringArray(R.array.normal); 
      String q = myString[rgenerator.nextInt(myString.length)];
      TextView tv = (TextView) findViewById(R.id.text1);
      tv.setText(q);
      }
  
          //selection of the spinner
          Spinner s = (Spinner) findViewById( R.id.spinner);
  
          // Application of the Array to the Spinner
              ArrayAdapter&lt;String&gt; spinnerArrayAdapter = new ArrayAdapter&lt;String&gt;(this,   android.R.layout.simple_spinner_item, situation);
              spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              s.setAdapter(spinnerArrayAdapter);
  
              s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
  
                  @Override
                  public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos, long id) {
  
                      MyScheduledActivity.this.mPos = pos;
                      MyScheduledActivity.this.mSelection = parent.getItemAtPosition(pos).toString();
  
                      //display toast                     
                      Toast.makeText(parent.getContext(), "The Situation is " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
  
                      if (mPos == 0){
  
                          Resources res = getResources();
                          myString = res.getStringArray(R.array.hardships); 
                          String q = myString[rgenerator.nextInt(myString.length)];
                          TextView tv = (TextView) findViewById(R.id.text1);
                          tv.setText(q);
  
                      }
  
                      if (mPos == 1){
  
                          Resources res = getResources();
                          myString = res.getStringArray(R.array.hardships); 
                          String q = myString[rgenerator.nextInt(myString.length)];
                          TextView tv = (TextView) findViewById(R.id.text1);
                          tv.setText(q);
  
                      }
  
                      else {
                          //do nothing
                           }
  
                  }
  
                  @Override
                  public void onNothingSelected(AdapterView&lt;?&gt; parentView) {
                  //do something else
                      return;
                  }
                  });
  
  
      //dismiss button - Thank You
      Button buttonDismiss = (Button)findViewById(R.id.dismiss);
  
      buttonDismiss.setOnClickListener(new Button.OnClickListener(){
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  finish();
              }});
  
  }
  
  @Override
  protected void onSaveInstanceState(Bundle outState) {
      outState.putStringArray(mSelection, situation);
      super.onSaveInstanceState(outState);
  }
  
  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
      situation = savedInstanceState.getStringArray("mSelection");
      super.onRestoreInstanceState(savedInstanceState);
  }
  
  
  }
  </code></pre>
  
  <p><a href="http://www.flickr.com/photos/58646431@N02/6001239313/" rel="nofollow noreferrer">This is how the activity looks</a></p>