How can I save the current selected spinner value, such that when I reopen the application the saved value is automatically selected by default?
My current code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( getBaseContext());
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("savedValue",spinner.getSelectedItem().toString());
String savedValue=spinner.getSelectedItem().toString();
for(int i=0;i<6;i++)
if(savedValue.equals(spinner.getItemAtPosition(i).toString()))
{
spinner.setSelection(i);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent){}
});