简体   繁体   中英

How do I keep a menu item group open when rotating android device

I've handled screen rotation config changes in the android manifest, which works for dialog themed activities, however for these menu groups, which open after selecting a menu item (in onOptionsItemSelected) still close when I rotate the screen. Can I handle these in onConfigurationChanged? Or is there a better way? I've attached the code that opens the submenu.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   if (item.getGroupId() == R.id.submenu) {
      if (item.getItemId() == this.submenu) {
         return true;
      }
      this.value = item.getItemId();
      item.setChecked(true);
      //do something with value
      return true;
   }
   //...
   return super.onOptionsItemSelected(item);
}

you will need to override

    @Override
 public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
 }

But to do this you must specify the configuration change you will handle yourself by adding to the manifest file on the Activity level the tag

     android:configChanges=["mcc", "mnc", "locale",
                             "touchscreen", "keyboard", "keyboardHidden",
                             "navigation", "orientation", "screenLayout",
                             "fontScale", "uiMode"]

on onConfigurationChanged save the state and reload it on onResume

This ended up being a very simple fix. I needed to add the line android:configChanges="orientation" to my activity in the AndroidManifest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM