使用菜单按钮启动 PreferenceActivity,使用后退按钮结束
我修改了 CommonsWare 的 prefenceActivity 应用程序,但遇到了问题。
我想使用菜单按钮启动 prefenceActivity,然后使用后退按钮完成它。
我已经通过菜单按钮启动了我的 prefenceActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
startActivity(new Intent(this, EditPreferences.class));
return(true);
}
它确实返回到应用程序的主视图,但我认为它还没有完成,因为当我再次按下菜单按钮时, prefenceActivity 不会弹出再次起来。我必须关闭我的应用程序,然后它只能再次运行一次。
当我按下后退按钮时,我已经尝试在我的 prefenceActivity 中完成它,但这也没有帮助。
我做错了什么?
感谢您的帮助!
I've modify a prefenceActivity application of CommonsWare but ran into a problem.
I would like to start a prefenceActivity with the menu button and just finish it with the back button.
I've got this to start my prefenceActivity via the menu button:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
startActivity(new Intent(this, EditPreferences.class));
return(true);
}
And it does go back to my main view of the app, but it isn't finished I think, because when I press the menu button again the prefenceActivity doesn't pop up agian. I have to close my app and than it works again only for 1 time only.
I've already tried to finish it in my prefenceActivity when I press the back button but this didn't help either.
What is it that i'm doing wrong?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想在按下菜单按钮时执行某些操作,您应该简单地实现 onKeyDown()/onKeyUp() 来像处理任何其他键一样处理菜单键。请务必返回 true 表示您正在使用它,这样默认的按键处理就不会执行(通常涉及显示菜单面板)。
不要通过实现 onCreateOptionsMenu() 或 onPrepareOptionsMenu() 来执行此操作。这些是您实现的挂钩,用于与选项菜单交互,而不是与菜单键交互。不保证这些将被调用与按下的菜单键有任何关系。碰巧它们经常是这样,但 API 中没有定义任何内容说明情况如此,而且事实上,情况显然并非如此。
作为一般规则,您绝对应该避免依赖这样的副作用(哦,当用户按下菜单键时,将调用未记录为直接与菜单键相关的 API)。根据定义,您依赖于实现细节,这很可能会困扰您。
例如,从 Android 3.0 开始,如果有操作栏,则在创建活动时立即调用 onCreateOptionsMenu() ,并且每当需要更新操作栏中的操作时就会调用 onPrepareOptionsMenu() 。这是因为选项菜单现在与操作栏集成了。
If you just want to do something when the menu button is pressed, you should simply implement onKeyDown()/onKeyUp() to handle the menu key like you would any other key. Be sure to return true to say you are consuming it so the default key handling is not executed (generally involving showing the menu panel).
Do NOT do this by implementing onCreateOptionsMenu() or onPrepareOptionsMenu(). These are hooks you implement to interact with the options menu, NOT with the menu key. There is no guarantee that these will be called in any relationship with the menu key being pressed. It happens that often they are, but there is nothing defined in the API saying this is so and indeed this explicitly is not the case.
As a general rule, relying on side-effects like this (oh when the user presses the menu key this API that is not documented as being directly tied to the menu key is invokved) is something you absolutely should avoid. You are by definition relying on implementation details, and there is a good chance this will bite you.
For example as of Android 3.0 when there is an action bar onCreateOptionsMenu() is called immediately when the activity is created and onPrepareOptionsMenu() is called whenever the actions in the action bar need to be updated. This is because the options menu is now integrated with the action bar.
使用
一个快速的解决方案(尽管我不建议这样做)是在每次按下菜单按钮时(在显示菜单之前) This 。 onCreateOptionsMenu 的问题是它仅在第一次创建菜单之前调用。因此,这并不是说活动没有完成,而是您用来启动活动的方法有问题
A quick solution (though I don't suggest it) is to use
This is called everytime the menu button is pressed, right before the menu is shown. The problem with onCreateOptionsMenu is it is only called before the first time the Menu is created. So this isn't so much that the activity isn't finishing, but a problem with the method you used to start the activity