Is there any way to detect that past launch have finished with force stop (terminate)? Not in the moment, just in next launch. Maybe resets someone temporary flag, state or other that can be checked. For example, I tried to check WorkInfo
from WorkManager
. But its requests survive after force stop.
This is exactly the information the ApplicationExitInfo
provides for API 30 and higher devices. Specifically the REASON_USER_REQUESTED
:
Application process was killed because of the user request, for example, user clicked the "Force stop" button of the application in the Settings, or removed the application away from Recents.
This is actually precisely what WorkManager uses to detect force stops on API 30 and higher by using getHistoricalProcessExitReasons
:
ActivityManager activityManager =
(ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ApplicationExitInfo> exitInfoList =
activityManager.getHistoricalProcessExitReasons(
null /* match caller uid */,
0, // ignore
0 // ignore
);
if (exitInfoList != null && !exitInfoList.isEmpty()) {
// The most recent exit is the first one, so we can just
// check that one
ApplicationExitInfo lastExit = exitInfoList.get(0);
if (info.getReason() == REASON_USER_REQUESTED) {
// You were force stopped, do your logic here
}
}
There's no direct equivalent on lower API levels, which is why WorkManager, as per that code linked above, sets an alarm very, very far in the future simply because a force stop will clear all alarms - the lack of an alarm is enough to somewhat reliably detect a force stop.