旋转 Android 设备时如何保持菜单项组打开
我已经在 android 清单中处理了屏幕旋转配置更改,该更改适用于对话框主题活动,但是对于这些菜单组,在选择菜单项(在 onOptionsItemSelected 中)后打开的菜单组在我旋转屏幕时仍然关闭。我可以在 onConfigurationChanged 中处理这些吗?或者有更好的方法吗?我已附上打开子菜单的代码。
@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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要重写,
但要做到这一点,您必须指定您将通过在 Activity 级别上的清单文件中添加 onConfigurationChanged 上的标记来自行处理的配置更改
,保存状态并在 onResume 上重新加载它
you will need to override
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
on onConfigurationChanged save the state and reload it on onResume
这最终是一个非常简单的修复。我需要将
android:configChanges="orientation"
行添加到 AndroidManifest 中的活动中。This ended up being a very simple fix. I needed to add the line
android:configChanges="orientation"
to my activity in the AndroidManifest.