-1

I am now learning android. I am working on an application, where in when i click on a value of Spinner it moves to the next Intent. And then when I come back to the old (first intent), i need to retain the selected item in that spinner. Please help me out. Thanks in advance

1 Answers1

0

Create a static variable in activity, declare it globally. Save the value of selected item on click of it. On onCreate() method of your activity, check whether the value is null, if not load that value in to spinner and make it selected. Also make static variable null once you use that.

UPDATE:

Create a global static variable as follows in your activity.

static String valueSelected=null;

in onCreate() method write the code as follows.

onCreate(){
     setContentView();
     ....
     if(valueSelected!=null){
          sp.setSelected(valueSelected);
          valueSelected=null;
     }

}

and in onClick() listener set the value to valueSelected as follows.

onClick(){
valueSelected=sp.spinner.getSelectedItem().toString();
}

Where sp is spinner

Vilas
  • 1,695
  • 1
  • 13
  • 13