androideventsbackonlongclicklistenerlong-click

Android long press system back button listener


I want to add a listener for when the android system back button is long pressed i.e. a long press version of Activity.onBackPressed

This only needs to work when my app is visible.

Chrome shows the history menu when back is long pressed so it must be possible but can't find a hook for it. Thanks


Solution

  • You can try overriding the onKeyLongPress method inside your Activity.

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            System.out.println("Back button long pressed");
            return true;
        }
        return super.onKeyLongPress(keyCode, event);
    }