How to exit an Android app programmatically?

前端 未结 29 977
孤城傲影
孤城傲影 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:23

    Accually there are two possible situations:

    1. You may want to exit from the activity
    2. Or you want to exit from the application

    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());
    

提交回复
热议问题