But when I close the app from recent apps by swiping it, onStop() or onDestroy() isn't called.
Methods of Activity
lifecycle that are to be called when Activity
is no longer visible are not guaranteed to be called when removed from the recent tasks (treat it as "soft" version of killing an app by the system due to low memory).
I need to know when the app is closed from recent apps to do something (e.g make user offline)
I suggest one of the following:
The Intent
passed to the method will help you recognize which component triggered the start request:
Intent
!= null, meaning the request has been received from a running Activity
instance
Intent
= null, meaning the request has been sent by the (newly created) Application
instance
* Not guaranteed to be called