1

I want to keep the spinner position when the screen is rotated

I've looked at a few answers on here and so far have the following

Aim - Store the position of the spinner

protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    flav1Spinner = (Spinner)findViewById(R.id.Combo_InvChoice1);
    Integer flav1 = flav1Spinner.getSelectedItemPosition();
    savedInstanceState.putInt("cho1", flav1);
}

Aim - Restore position

 protected void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculation);
    Locale.setDefault(Locale.US);
   if (savedInstanceState != null){
        flav1Spinner = (Spinner)findViewById(R.id.Combo_InvChoice1);
        Integer flav1 = savedInstanceState.getInt("cho1");
        flav1Spinner.setSelection(flav1);
    }

However while debugging I can see that the Integer Flav1 is getting a value assigned to it and the digit is being retrieved, however the spinner isn't changing it's position. Any suggestions?

Sjharrison
  • 729
  • 3
  • 16
  • 39

1 Answers1

0

You can do it by onSaveInstanceState(), As below

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("yourSpinner", yourSpinner.getSelectedItemPosition());
    // do this for each of your Spinner

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        yourSpinner.setSelection(savedInstanceState.getInt("yourSpinner", 0));
        // do this for every text views
    }
}

Check Saving Activity state, it will help you to get more details.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142