How to exit when back button is pressed?

前端 未结 12 1222
名媛妹妹
名媛妹妹 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 11:58

    Why wouldn't the user just hit the home button? Then they can exit your app from any of your activities, not just a specific one.

    If you are worried about your application continuing to do something in the background. Make sure to stop it in the relevant onPause and onStop commands (which will get triggered when the user presses Home).

    If your issue is that you want the next time the user clicks on your app for it to start back at the beginning, I recommend putting some kind of menu item or UI button on the screen that takes the user back to the starting activity of your app. Like the twitter bird in the official twitter app, etc.

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

    To exit from an Android app, just simply use. in your Main Activity, or you can use Android manifest file to set

    android:noHistory="true"
    
    0 讨论(0)
  • 2020-11-27 12:03

    I modified @Vlad_Spays answer so that the back button acts normally unless it's the last item in the stack, then it prompts the user before exiting the app.

    @Override
    public void onBackPressed(){
        if (isTaskRoot()){
            if (backButtonCount >= 1){
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }else{
                Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
                backButtonCount++;
            }
        }else{
            super.onBackPressed();
        }
    }
    
    0 讨论(0)
  • 2020-11-27 12:03

    you can simply use this

     startActivity(new Intent(this, Splash.class));
     moveTaskToBack(true);
    

    The startActivity(new Intent(this, Splash.class)); is the first class that will be lauched when the application starts

    moveTaskToBack(true); will minimize your application

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

    A better user experience:

    /**
     * Back button listener.
     * Will close the application if the back button pressed twice.
     */
    @Override
    public void onBackPressed()
    {
        if(backButtonCount >= 1)
        {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
        else
        {
            Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
            backButtonCount++;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 12:11

    finish your current_activity using method finish() onBack method of your current_activity

    and then add below lines in onDestroy of the current_activity for Removing Force close

    @Override
    public void onDestroy()
    {
        android.os.Process.killProcess(android.os.Process.myPid());
        super.onDestroy();
    }
    
    0 讨论(0)
提交回复
热议问题