Android - onBackPressed() not working

与世无争的帅哥 提交于 2019-11-26 23:02:25
Jorgesys

Are you using onKeyUp()?

Use just onKeyDown() in Android 1.x or onBackPressed() in Android 2.x

Some quick searching suggests you should place the Back intercept during onKeyUp(): http://developer.android.com/sdk/android-2.0.html. It's worth a try. The following code is directly from the site:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        event.startTracking();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
            && !event.isCanceled()) {
        // *** DO ACTION HERE ***
        return true;
    }
    return super.onKeyUp(keyCode, event);
}
ashakirov

You should call parent constructors.

In onKeyDown() method call

super.onKeyDown();

and in onBackPressed()

super.onBackPressed();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!