我需要完成一个应用程序,当它进入后台,我使用方法finishAffinity()
但似乎它不起作用,有人可以告诉我另一种选择
@Override
protected void onPause() {
finishAffinity()
super.onPause();
}
这是answer
finishAffinity()不用于“关闭应用程序”。它用于从当前任务中删除属于特定应用程序的许多活动(可能包含属于多个应用程序的活动)。
即使您完成了应用程序中的所有活动,托管应用程序的操作系统进程也不会自动消失(就像调用System.exit()时一样)。 Android最终会在它进入时终止你的进程。你无法控制这一点(这是故意的)。
你可以用它
public void endTask() {
// Is the user running Lollipop or above?
if (Build.VERSION.SDK_INT >= 21) {
// If yes, run the fancy new function to end the app and
// remove it from the task list.
finishAndRemoveTask();
} else {
// If not, then just end the app without removing it from
// the task list.
finish();
}
}
Source并阅读更多