Intercepting the back button

后端 未结 4 1392
自闭症患者
自闭症患者 2020-12-10 01:39

I\'m writing an application which will have two Activities, when the user presses the back button on the second activity a dialog should pop up asking the user to confirm th

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 01:53

    Simply override the onKeyDown(int, KeyEvent) method in your activity and look for the back button. Return true so that the event is consumed.

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //Do something here
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

提交回复
热议问题