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

Android Studio - How to ( close, quit, exit, kill, etc.) an application

本问题已经有最佳答案,请猛点这里访问。

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

问题:当用户在我的应用程序中按下退出按钮时,我想杀死所有的应用程序。有一些方法,如finish();或system.exit(0);如果我使用MainActivity,它们就是工作。我想要这个,如果我的主活动或者A活动或者B活动不重要的话,当我按下退出按钮的时候,只需要终止所有的应用程序并返回电话菜单。


这将做的把戏 P / < >

1
finishAffinity();

看看这对deails API的描述: P / < >

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.