如何完全关闭Android应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21130493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:01:49  来源:igfitidea点击:

how to close android app completely

androidback-buttonback-button-controlclose-application

提问by Brett

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 the previous activity, and when it gets a null value it redirects to login screen. Now my login screen appears again and again. I want to close the app completely when I press the back button but don't know how.

我在onCreateOptionsMenu. 注销工作正常,但是当我再次按下后退按钮时,它会将我带到上一个活动,当它获得空值时,它会重定向到登录屏幕。现在我的登录屏幕一次又一次地出现。我想在按下后退按钮时完全关闭应用程序,但不知道如何关闭。

Here is my code to handle the back button press in the login screen:

这是我在登录屏幕中处理后退按钮的代码:

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
        .setTitle("BidNEMO")
        .setMessage("Are you sure you want to exit?")
        .setNegativeButton(android.R.string.no, null)
        .setPositiveButton(android.R.string.yes, new OnClickListener() {
         public void onClick(DialogInterface arg0, int arg1) {
         Main.super.onBackPressed();
         finish();
         }
         }).create().show();
     }

please help guyss..

请帮助伙计们..

回答by ryderz8

To Quit Applicationon Button click use this code :

Quit Application在按钮上单击,请使用以下代码:

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.

要杀死完整的应用程序并将其从Running应用程序列表中删除,请通过其pid(令人讨厌的)杀死应用程序...在上述代码之前使用此行。

int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);

回答by SachiN Ware.

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.

如果你想完全关闭应用程序,你应该使用finishAffinity();而不是finish(). 它将清除以前由应用程序打开的所有活动堆栈。

回答by Roy Doron

For Xamarin Users:

对于 Xamarin 用户:

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

put it in your OnDestroy()function.

把它放在你的OnDestroy()函数中。

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!

这是该问题的唯一解决方案!

回答by Mick Ashton

For API 21 and up

对于 API 21 及更高版本

finishAndRemoveTask()

finishAndRemoveTask()

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

您可以调用它来完全关闭应用程序。所有活动都finish()将从任务列表中删除。

回答by chiru

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

回答by Pavel Polushkin

Even if it looks ok killing process will cause you headache in future. You can ensure this by following use case:

即使看起来不错,杀死过程也会让你在未来头疼。您可以通过以下用例来确保这一点:

  1. call several activities to populate activities stack
  2. call android.os.Process.killProcess(pid);
  3. open your application
  1. 调用多个活动来填充活动堆栈
  2. 调用 android.os.Process.killProcess(pid);
  3. 打开您的应用程序

OBSERVED: Your application opens not from main activity in inconsistent way. This is because Android consider killing of process like crash.

观察:您的应用程序不是从主要活动以不一致的方式打开的。这是因为 Android 考虑像崩溃一样杀死进程。

You can find more detailed information here: Is quitting an application frowned upon?

您可以在此处找到更多详细信息: 是否不赞成退出应用程序?

回答by AlienKilleR

android.os.Process.killProcess(android.os.Process.myUid());

I think this is better. How about it ?

我觉得这样更好。这个怎么样 ?