以编程方式退出似乎成功,但 "Force stop" 按钮仍处于活动状态

Exiting programmatically seems successful, but "Force stop" button is still active

我正在尝试以编程方式退出我的应用程序,所以我调用这些行:

my_activity.finishAndRemoveTask();
context.stopService(new Intent(context, mysvc.class));

应用程序似乎正在关闭:

com.my.package I/my_activity: onDestroy
I/mysvc: onDestroy

所以activity和服务似乎都被破坏了。

但是,当我签入 Settings/Apps/App info 时,我的应用程序似乎仍然“可强制停止”(即强制停止按钮仍然有效)。

当我按下这个“强制停止”按钮时:

D/AppInfoDashboard: Stopping package com.my.package
I/ActivityManager: Force stopping com.my.package appid=xy, user=xy: from pid xy
I/ActivityManager: Killing xy:com.my.package/xy (adj xy): stop com.my.package
D/AppActionButtonControl: Sending broadcast to query restart status for com.my.package
D/AppActionButtonControl: Sending broadcast to query restart status for com.my.package
D/AppActionButtonControl: Got broadcast response: Restart status for com.my.package false
D/AppActionButtonControl: Got broadcast response: Restart status for com.my.package false

按下此按钮后,它变为非活动状态,我相信我的 com.my.package 应用程序已成功(并完全)终止。

所以我的问题是,如果我上面的那些代码行没有做到这一点,我该如何以编程方式实现这一点?还有:如果 Android 明确要求退出,为什么 Android 让我的应用程序保持某种准 运行 状态?

请注意,我不想在退出时终止我的应用程序。当用户从菜单中选择“退出”选项时,我想优雅地终止它。

I believe my com.my.package application is successully (and fully) terminated.

比这多 很多。 “强制停止”按钮不是您希望用户执行的那种操作。

how can I reach this programmatically, if those lines of my code above doesn't make this?

您不能强制停止您自己的应用程序。

why Android keeps my application in some quasi-running state if it explicitly asks for exit?

引用 the documentation:

An unusual and fundamental feature of Android is that an application process's lifetime is not directly controlled by the application itself. Instead, it is determined by the system through a combination of the parts of the application that the system knows are running, how important these things are to the user, and how much overall memory is available in the system.

在 Android 应用程序中有一个“退出”选项是非常不寻常的。作为“退出”逻辑的实现,您目前正在做的事情并不可怕,尽管用户可能会对应用程序的任务消失感到困惑。