如何在应用程序进入后台时完成它

问题描述 投票:1回答:1

我需要完成一个应用程序,当它进入后台,我使用方法finishAffinity()但似乎它不起作用,有人可以告诉我另一种选择

  @Override
    protected void onPause() {
    finishAffinity()
    super.onPause();
}
java android android-lifecycle android-ondestroy systemexit
1个回答
1
投票

这是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并阅读更多

© www.soinside.com 2019 - 2024. All rights reserved.