how to close android app completely

前端 未结 7 1735
一个人的身影
一个人的身影 2020-12-03 01:59

I created an android application with a logout option in onCreateOptionsMenu. The Logout works perfectly but when I press the back button again it takes me to t

相关标签:
7条回答
  • 2020-12-03 02:09

    To Finish an Activity I'm using this code:

    public void appExit () {
        this.finish();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }  //close method
    

    or kill Process with this code:

    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    
    0 讨论(0)
  • 2020-12-03 02:11
    android.os.Process.killProcess(android.os.Process.myUid());
    

    I think this is better. How about it ?

    0 讨论(0)
  • 2020-12-03 02:13

    For API 21 and up

    finishAndRemoveTask()

    You can call this to close the app completely. All activities will finish() and the app is removed from the task list.

    0 讨论(0)
  • 2020-12-03 02:17

    For Xamarin Users:

    int pid = Android.OS.Process.MyPid();
    Android.OS.Process.KillProcess(pid);
    

    put it in your OnDestroy() function.

    Edit:

    After investigating it thoroughly, I found out that even the above code I wrote does not "Kill" the app totally (deleting it from task manager - "recent apps"). Eventually, after a lot of code tryouts, I managed to figure something out, Overriding "Finish" functions with this code:

    public override void Finish()
        {
            if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                base.FinishAndRemoveTask();
            }
            else
            {
                base.Finish();
            }
        }
    

    this is the sole solution for that question!

    0 讨论(0)
  • 2020-12-03 02:19

    To Quit Application on Button click use this code :

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
    

    Try it..

    To kill the complete app and remove it from Runningapp list kill the app through its pid(its nasty)... use this lines before above code.

    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    
    0 讨论(0)
  • 2020-12-03 02:29

    If you want to close application completely you should use finishAffinity(); instead of finish() . It will clear all stack of activities previously opened by an application.

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