Note that there are some explanatory texts on larger screens.

plurals
  1. POPreserve MenuItem checkbox status on rotate events
    primarykey
    data
    text
    <p>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.</p> <p>What I want to achieve is to show that box checked again after rotating the device.</p> <p>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.</p> <p>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.</p> <p>I no have a clue of what to do. :(</p> <p>The code:</p> <pre><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); } </code></pre> <p>EDIT: Those are the kind of checkbox that I'm using, I'm not setting manually any icon.</p> <p><img src="https://i.stack.imgur.com/Gdo1S.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/nF70T.png" alt="enter image description here"></p> <p>EDIT2:</p> <p>OK, looks like that <code>android:configChanges="orientation|screenSize"</code> does the trick, but it stops using the layout for landscape mode and uses the defalut one instead. And <code>android:configChanges="orientation"</code> without "screenSize" does nothing.</p> <p>Edit3:</p> <p>Shameless auto bump. I'm still stuck. :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload