How to exit when back button is pressed?

前端 未结 12 1289
名媛妹妹
名媛妹妹 2020-11-27 11:47

When a user presses the back button on an intent, the application should quit. How can I ensure the application quits when the back button is pressed?

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 12:13

    In my Home Activity I override the "onBackPressed" to:

    @Override
    public void onBackPressed() {
       Intent intent = new Intent(Intent.ACTION_MAIN);
       intent.addCategory(Intent.CATEGORY_HOME);
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(intent);
     }
    

    so if the user is in the home activity and press back, he goes to the home screen.

    I took the code from Going to home screen Programmatically

提交回复
热议问题