Android的菜单选项上的复选框方向改变选中修复 [英] Android Menu Options checkboxes unchecked on orientation change fix
问题描述
在我的应用程序,当选项溢出菜单进行检查,然后将方向改变时,他们变得听之任之。任何方式解决这一问题?
我知道 onSavedInstance
应能帮助我在这里,但我不知道如何实现它在我的情况。
In my app, when options are checked in the overflow menu, then the orientation is changed, they become unchecked. Any way to fix this?
I know onSavedInstance
should be able to help me here, but I don't know how to implement it in my case.
下面是我的溢出复选框是如何在主要活动处理为例
Here's an Example of how my overflow checkboxes are handled in the main activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.subtract_option:
if (item.isChecked()){
item.setChecked(false);
subtractMode = false;
} else{
item.setChecked(true);
subtractMode = true;
}
return true;
default:
return super.onMenuItemSelected(featureId, item);
所以,我将如何贯彻落实 onSavedInstanceState
在这种情况下?
推荐答案
它与Android如何处理方向改变的事情。它实际上是杀死你的活动,并重新启动它。有两种方法来解决它。
It has to do with how Android handles orientation changes. It actually kills your activity and restarts it. There's two ways to fix it.
1)的onSaveInstanceState实施,编写所有状态的对象(包括复选框的值),并实施onRestoreInstanceState的逆过程。
1)Implement onSaveInstanceState, write all your state to an object (including the values of checkboxes) and implement onRestoreInstanceState to reverse the process.
2)添加机器人:configChanges =方向。这将停止压倒一切的行为。这也将打破布局的自动载,如果你有不同的景观ANDD纵向布局。
2)Add android:configChanges="orientation". This will stop the overriding behavior. It will also break the automatic reloading of layouts if you have separate landscape andd portrait layouts.
我推荐的路线2,如果你的没有了方向不同的布局。如果你这样做,你的应用程序很简单,我建议路线1.如果你需要和你的应用程序是复杂的你拧并在痛苦的世界。
I recommend route 2 if your don't have separate layouts for the orientations. If you do and your app is simple, I suggest route 1. If you need that and your app is complex you're screwed and in for a world of pain.
这篇关于Android的菜单选项上的复选框方向改变选中修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!