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
Asked
Active
Viewed 593 times
1 Answers
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
-
please can you explain lil bit more? do i need to pass tat variable in setSelected? – Android-Learner Jan 28 '15 at 04:46
-
heyyy thank you... I got it :) :) Thanks a lot :) :D – Android-Learner Jan 28 '15 at 04:48
-
Retaining in what context? When it is retaining and when it is not retaining? – Vilas Jan 28 '15 at 05:22
-
Check the updated answer. Hope its helpfull. – Vilas Jan 28 '15 at 06:20