0

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.

enter image description here enter image description here

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. :(

Community
  • 1
  • 1
Deses
  • 1,074
  • 2
  • 12
  • 25

2 Answers2

0

Looked like a common orientation change - state saving problem to me. There are already great solutions for this problem, such as the ones provided in the following links:

How to save state during orientation change in Android

Saving Activity state in Android

Community
  • 1
  • 1
ecem
  • 3,574
  • 3
  • 27
  • 40
  • I've already been on those links, and didn't do much good. :( Also, I've explained better my problem. – Deses Feb 13 '13 at 23:42
  • @ecem the point is that you **should not** have to save the **view state** manually. Put a single CheckBox in a layout and create an empty activity with it, the state will be preserved across rotation and even Home button press and selecting from Recents. – TWiStErRob Oct 21 '14 at 12:36
0

From what I have experienced, having the following in my manifest file retains the state.

" android:configChanges="orientation|screenSize"/>

Saw your edits later. May be this is what you are doing wrong:

MenuItem's checked state is not shown correctly by its icon

Community
  • 1
  • 1
wraith
  • 391
  • 1
  • 14