How to exit an Android app programmatically?

前端 未结 29 971
孤城傲影
孤城傲影 2020-11-28 03:54

I am sure this question has been asked number of times because I read a few. My client wants me to put a button into his app where users can click and exit. I have read this

相关标签:
29条回答
  • 2020-11-28 04:04

    just use the code in your backpress

                        Intent startMain = new Intent(Intent.ACTION_MAIN);
                        startMain.addCategory(Intent.CATEGORY_HOME);
                        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(startMain);
    
    0 讨论(0)
  • 2020-11-28 04:05

    If someone still wonders, for Xamarin.Android (in my case also Monogame running on it) the command FinishAndRemoveTask() on Activity does the job very well!

    0 讨论(0)
  • 2020-11-28 04:06

    If you want to exit from your application, use this code inside your button pressed event:

    public void onBackPressed() {
      moveTaskToBack(true);
      android.os.Process.killProcess(android.os.Process.myPid());
      System.exit(1);
    }
    
    0 讨论(0)
  • 2020-11-28 04:07

    Link this method to your quit/exit button

    public void quitGame(View view) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                finishAndRemoveTask();
            } else {
                finish();
            }
        }
    
    0 讨论(0)
  • 2020-11-28 04:08

    How about this.finishAffinity()

    From the docs,

    Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch. Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.

    0 讨论(0)
  • 2020-11-28 04:08

    Just call this:

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