How to catch event with hardware back button on android?

前端 未结 3 1956
谎友^
谎友^ 2020-12-29 18:40

How to catch event with hardware back button on android ? I need to supress user to go back and I when click on back button on phone to show message and not to go on previou

3条回答
  •  萌比男神i
    2020-12-29 19:28

    You can suppress user "back" action with onKeyDown() in your activity like this:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
            //do actions like show message
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
    

提交回复
热议问题