I'm trying to save user selected sort option in the SharedPreference
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.submenu_sort_alpha:
displayListView(db.getAllPlaces(DBAdapter.KEY_LABEL));
item.setChecked(true);
prefs.edit().putInt("com.xxx.yyy.selectedSort",
R.id.submenu_sort_alpha);
return true;
case R.id.submenu_sort_alpha_desc:
displayListView(db.getAllPlaces(DBAdapter.KEY_LABEL + " DESC"));
item.setChecked(true);
prefs.edit().putInt("com.xxx.yyy.selectedSort",
R.id.submenu_sort_alpha_desc);
return true;
...
And restore it when menu is created
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
int selectedSort = prefs.getInt("com.xxx.yyy.selectedSort",
R.id.submenu_sort_alpha);
menu.findItem(selectedSort).setChecked(true);
But the code is not working as expected. (Or should it work?)
If it should be working, is it good habit to save item id in SharedPreference? Or should I just assign unique numbers to each option, save it in SharedPreference and then set the radio button as checked by checking with long 'if else' code?
0 comments:
Post a Comment