[英]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.