I have a MenuItem from SherlockActionBar in my Menu that has a checkbox, but when I rotate the device, the checkbox is still active, but the icon on the right of the label is unchecked.
What I want to achieve is to show that box checked again after rotating the device.
So far I've been trying to fix it with SharedPreferences, flags, storing "Menu" (from onCreateOptionsMenu) to a variable and looking for the id, onPrepareOptionsMenu, but nothing is doing what I want.
What I'm doing right now is storing a status flag of the MenuItem on "onOptionsItemSelected", then on "onPrepareOptionsMenu" check the value of the flag and setting the check... But that is causing even more trouble.
I no have a clue of what to do. :(
The code:
private boolean lockFlag;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_lock:
if (item.isChecked()) {
item.setChecked(false);
lockFlag = false;
} else {
item.setChecked(true);
lockFlag = true;
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (lockFlag) {
menu.findItem(R.id.menu_lock).setChecked(true);
} else {
menu.findItem(R.id.menu_lock).setChecked(false);
}
return true;
}
private void savePrefs() {
getSharedPreferences("LOCKBARS", MODE_PRIVATE).edit().putBoolean("lockbars", lockFlag).commit();
}
private void loadPrefs() {
this.lockFlag = getSharedPreferences("LOCKBARS", MODE_PRIVATE).getBoolean("lockbars", false);
}
EDIT: Those are the kind of checkbox that I'm using, I'm not setting manually any icon.
EDIT2:
OK, looks like that android:configChanges="orientation|screenSize"
does the trick, but it stops using the layout for landscape mode and uses the defalut one instead.
And android:configChanges="orientation"
without "screenSize" does nothing.
Edit3:
Shameless auto bump. I'm still stuck. :(