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
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
System.exit(1);
Use this Code it's much useful, and you can exit all of the activities.
Accually there are two possible situations:
You can exit from the activity using following code:
var intent = new Intent(Intent.ActionMain);
intent.AddCategory(Intent.CategoryHome);
intent.SetFlags(ActivityFlags.NewTask);
startActivity(intent);
finish();
But this will not kill the underlying activities in the same application. This will just minimize the application.
If you want to exit from application use the following code to end its process:
android.os.Process.killProcess(android.os.Process.myPid());
for mono development just use
process.KillProcess(Process.MyPid());
Try this on a call. I sometimes use it in onClick of a button.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
It instead of closing your app , opens the dashboard so kind of looks like your app is closed.
read this question for more clearity android - exit application code
Instead of System.exit(1) Just use System.exit(0)
The finishAffinity method, released in API 16, closes all ongoing activities and closes the app:
this.finishAffinity();
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.
Since API 21, you can use:
finishAndRemoveTask();
Finishes all activities in this task and removes it from the recent tasks list.
Alternatives:
getActivity().finish();
System.exit(0);
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
Process.sendSignal(Process.myPid(), Process.SIGNAL_KILL);
Intent i = new Intent(context, LoginActivity.class);
i.putExtra(EXTRA_EXIT, true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(i);
Source: How to quit android application programmatically
Hope it helps! Good Luck!
Achieving in Xamarin.Android:
public override void OnBackPressed()
{
MoveTaskToBack(true);
Process.KillProcess(Process.MyPid());
Environment.Exit(1);
}