How to know when an app is killed after being in recents for a long time?

Viewed 52

Which function is called when an app is killed by keeping it in recent apps list for a long time?

I am not asking about when an app is killed by swiping app from the recents list.

2 Answers

There is no such function in Android.

When user closes the app, the process is terminated with no notice. Even Activity's onDestroy method is not guaranteed to be called. Only when you explicitly call finish().

If you use ViewModel that is tied to the starting Activity's lifecycle, you can try to use onCleared() method, which is called always when ViewModel is no longer used and will be destroyed. See: https://developer.android.com/topic/libraries/architecture/viewmodel

There is no such function.

At the point when the system decides to kill your process, it will do so immediately without calling any more of your code beforehand.

Another answer suggests using ViewModel, but this won't work if you actually need a reliable signal. It is not guaranteed to trigger, and it may trigger in other circumstances (such as the user backing out of the activity, but the app process not being killed).

Related