How to exit when back button is pressed?

前端 未结 12 1223
名媛妹妹
名媛妹妹 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条回答
  • 2020-11-27 12:12

    First of all, Android does not recommend you to do that within the back button, but rather using the lifecycle methods provided. The back button should not destroy the Activity.

    Activities are being added to the stack, accessible from the Overview (square button since they introduced the Material design in 5.0) when the back button is pressed on the last remaining Activity from the UI stack. If the user wants to close down your app, they should swipe it off (close it) from the Overview menu.

    Your app is responsible to stop any background tasks and jobs you don't want to run, on onPause(), onStop() and onDestroy() lifecycle methods. Please read more about the lifecycles and their proper implementation here: http://developer.android.com/training/basics/activity-lifecycle/stopping.html

    But to answer your question, you can do hacks to implement the exact behaviour you want, but as I said, it is not recommended:

    @Override
     public void onBackPressed() {
    
    // make sure you have this outcommented
    // super.onBackPressed();
     Intent intent = new Intent(Intent.ACTION_MAIN);
     intent.addCategory(Intent.CATEGORY_HOME);
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(intent);
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-27 12:13

    The app will only exit if there are no activities in the back stack. SO add this line in your manifest android:noHistory="true" to all the activities that you dont want to be back stacked.And then to close the app call the finish() in the OnBackPressed

    <activity android:name=".activities.DemoActivity"
                android:screenOrientation="portrait"
                **android:noHistory="true"**
                />
    
    0 讨论(0)
  • 2020-11-27 12:13

    Use onBackPressedmethod

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }
    

    This will solve your issue.

    0 讨论(0)
  • 2020-11-27 12:17

    Add this code in the activity from where you want to exit from the app on pressing back button:

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exitFromApp();
    }
    
    private void exitFromApp() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }
    
    0 讨论(0)
  • 2020-11-27 12:22

    Immediately after you start a new activity, using startActivity, make sure you call finish() so that the current activity is not stacked behind the new one.

    EDIT With regards to your comment:

    What you're suggesting is not particularly how the android app flow usually works, and how the users expect it to work. What you can do if you really want to, is to make sure that every startActivity leading up to that activity, is a startActivityForResult and has an onActivityResult listener that checks for an exit code, and bubbles that back. You can read more about that here. Basically, use setResult before finishing an activity, to set an exit code of your choice, and if your parent activity receives that exit code, you set it in that activity, and finish that one, etc...

    0 讨论(0)
提交回复
热议问题