在后台运行时如何完成应用程序

时间:2019-03-04 23:38:42

标签: java android android-lifecycle android-ondestroy systemexit

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

  @Override
    protected void onPause() {
    finishAffinity()
    super.onPause();
}

1 个答案:

答案 0 :(得分:1)

这里是answer

  

finishAffinity()不用于“关闭应用程序”。它用于从当前任务中删除许多属于特定应用程序的活动(其中可能包含属于多个应用程序的活动)。

     

即使您完成了应用程序中的所有活动,托管应用程序的OS进程也不会自动消失(就像调用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 并阅读更多内容