简体   繁体   English

旋转android设备时如何保持菜单项组打开

[英]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. 我已经处理了Android清单中的屏幕旋转配置更改,该更改适用于以对话框为主题的活动,但是对于这些菜单组,当我旋转屏幕时,在选择菜单项(在onOptionsItemSelected中)后打开的菜单组仍然关闭。 Can I handle these in onConfigurationChanged? 我可以在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 on onConfigurationChanged保存状态并在onResume上重新加载

This ended up being a very simple fix. 最终这是一个非常简单的修复。 I needed to add the line android:configChanges="orientation" to my activity in the AndroidManifest. 我需要在AndroidManifest中的活动中添加android:configChanges="orientation"行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Android模拟器模拟旋转设备? - How do I simulate rotating the device with Android emulator? 设备进入睡眠模式时如何保持android应用打开 - how to Keep android app open when device goes to sleep mode Android-按下硬件菜单按钮后如何打开菜单? - Android - how do I open a menu when hardware menu button is pressed? 如何在 Kotlin 中的菜单项选择上打开一个新页面? - How do I open a new page on menu item select in Kotlin? 如何制作android菜单,以及在此菜单中选择一个项目时如何打开新屏幕 - how to make android menu , and how to open new screen when choose an item in this menu 旋转屏幕时如何保持android搜索对话框打开? - How to keep android search dialog on when rotating screen? 如何在Android中的菜单项上添加自定义字体? - How do i add custom font on menu item in Android? 旋转android设备时如何设置全屏视频 - how to set full screen video when rotating android device 选择项目后如何保持抽屉菜单打开? - How to keep drawer menu open after item selection? 在Android中使用矩阵旋转点时,为什么会得到不同的线长?我该如何解决? - Why do i get differing line lengths when rotating points with matrices in Android and how do i fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM