Android Studio-如何(关闭,退出,退出,终止等)应用程序

Ibr*_*LAT -1 android

首先,我已经尝试了有关此问题的几乎所有解决方案,但找不到解决方案。

问题: 当用户在我的应用程序中按退出按钮时,我想杀死所有应用程序。有一些方法如finish();。System.exit(0); 如果我在MainActivity上使用,它们将起作用。我想要这个,如果我按下退出按钮时MainActivity或A Activity或B Activity无关紧要哪个Activty,只需杀死所有应用程序并返回手机菜单即可。

Sre*_*ari 7

这将解决问题

finishAffinity();
Run Code Online (Sandbox Code Playgroud)

检查是否失败:API描述

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.