Android的后退按钮code永远不会执行 [英] Android Back button code never executed
问题描述
我在我的活动如下code。我必须设置为所有这些方法断点。当我preSS菜单按钮,进入onKeyDown和的onkeyup。当我preSS后退按钮,应用程序关闭和电话回到主屏幕不传递任何我的断点。
I have the following code in my Activity. I have Breakpoints set to all of these methods. When I press the Menu button, it enters onKeyDown and onKeyUp. When I press the back button, the app closes and phone goes back to home screen without passing any of my breakpoints.
编辑:
这可能要根据多数来源工作:
This could should work according to most sources:
@Override
public void onBackPressed() {
SwitchToMenuState();
}
然而,事实并非如此。我曾尝试以下code块的所有排列,并且他们都没有得到过名为:
However, it doesn't. I have tried all permutations of the following code blocks, and neither of them ever get called:
@Override
public void finish()
{
SwitchToMenuState();
}
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
SwitchToMenuState();
}
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
SwitchToMenuState();
}
return true;
}
我使用的是Android 2.3和加工,但似乎在所有涉及到处理它不会。 Eclipse的告诉我,我重写android.app.Activity.onBack pressed。
I am using Android 2.3 and Processing, although it doesn't seem to be related to Processing at all. Eclipse tells me that I am overriding android.app.Activity.onBackPressed.
推荐答案
所有你需要的是的onkeydown
All you need is onKeyDown
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Your code here
return true;
}
return super.onKeyDown(keyCode, event);
}
我想这是因为你没有返回超级一部分。这个工作对我来说,你不需要onback pressed或等的onkeyup
I think it's because you're not returning the super part. This works for me, you don't need the onbackpressed or onkeyup etc.
这篇关于Android的后退按钮code永远不会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!