Android - onBackPressed() not working

前端 未结 3 586
感动是毒
感动是毒 2020-11-28 14:49

I have an application building against Android 2.1 and I want to override the back button.

I have followed the example here:

http://android-developers.blogsp

相关标签:
3条回答
  • 2020-11-28 15:08

    You should call parent constructors.

    In onKeyDown() method call

    super.onKeyDown();
    

    and in onBackPressed()

    super.onBackPressed();
    
    0 讨论(0)
  • 2020-11-28 15:17

    Are you using onKeyUp()?

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

    0 讨论(0)
  • 2020-11-28 15:33

    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);
    }
    
    0 讨论(0)
提交回复
热议问题