2

I have a Spinner with a bunch of state names. In onCreate(), I set it to a default value. The index 0 in the Spinner array is "Alabama"

String state = "California"; //preset to this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_state_and_drivers_license);

    statesSpinner = (Spinner)findViewById(R.id.states_spinner);
    adapter = (ArrayAdapter<String>)statesSpinner.getAdapter();
    statesSpinner.setSelection(adapter.getPosition(state));

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, RESULT_CAMERA);    
}

However, after onResult, the Spinner is once again set to "Alabama". Meaning it reverts back to index 0 of the array, even though I thought it should keep its existing selected value.

Edit: I put setSelection(position) into onCreate, onResume, and onDestroy. Still, when I return from the camera intent, the spinner still resets and does not go to my selection.

4 Answers4

0

Its possible that calling the Camera Activity is not saving your state, in essence, calling onDestroy. I am not to sure. This other question, which also deals with camera intent had a similar issue. I would add a log in onDestroy, and check if its being called when calling the camera Activity. This other question also makes a bit of sense because normally your state should remain the same when calling a new Activity.

After some quick research it seems because of orientation change and possible memory issues, your state cannot be saved. You can also just do what the other answer says because since its preset, making sure its always that on onResume will be a easy workaround.

Community
  • 1
  • 1
Andy
  • 10,553
  • 21
  • 75
  • 125
0

Its really simple, same issue I resolved by follow this post really help me.

many stuffs lose there (like picked image,spinner etc..) state, you can recover simply by following link.

How do I preserve the state of a selected spinner/dropdown item on orientation change?

Thanks,

Community
  • 1
  • 1
Ronak Amlani
  • 654
  • 7
  • 15
-1
static int position=-1;
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent,
                        View view, int arg2, long arg3) {
                   position=arg2;  
                }
                public void onNothingSelected(AdapterView<?> arg0) {
                }
            });
    @Override
    protected void onResume() {
        super.onResume();
        if(position!=-1){
        spinner.setSelection(position);
        }
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • Instead of position, could I use a String for the state name, then find the position of that state? This is so I know what is pre-selected without having to count in my String array. –  Jul 12 '13 at 17:23
-1
static int POSITION =0;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
              POSITION=pos;
}

public void onNothingSelected(AdapterView<?> parent) {

}
});

@Override
public void onResume() {
    super.onResume();       
    spinner.setSelection(POSITION);
}
Iamat8
  • 3,888
  • 9
  • 25
  • 35