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
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);
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!
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);
}
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();
}
}
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.
Just call this:
finishAffinity();