Does the Android system notifies us when it kills our app service in any circumstance, or does it silently do the job? In case it notifies us, where & how do we catch it or know it?
Asked
Active
Viewed 78 times
2 Answers
1
Either scenario is possible. In conventional cases, when your process is terminated to free up system RAM, onDestroy()
should be called on your service. But, there are cases when this will not occur, such as:
- an emergency need for RAM (e.g., incoming phone call and we are short on system RAM)
- Force Stop in Settings
- if your service throws an unhandled exception
Ignoring the exception scenario, either onDestroy()
should be called or your process should be terminated.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
-
What if onDestroy() is not called? Is our app notified in any other way, eg. through broadcast or something else? i.e. is there a way to know whether it has been killed sure shottedly? – Osama Mohammed Shaikh May 20 '16 at 15:02
-
@OsamaMohammedShaikh: If you are not called with `onDestroy()` and you have not thrown an unhandled exception, your app's process is being terminated, without warning. – CommonsWare May 20 '16 at 15:31
-
Got your point! Either thing will happen. onDestroy() will be called by the system if there is no emergency thereby notifying us or the whole process itself will be terminated without warning leaving no chance of knowing whether app service was killed. So in most cases we will be notified except in extreme cases where whole process itself is terminated. So we are safe until resources run out. Right? – Osama Mohammed Shaikh May 20 '16 at 17:30
-
@OsamaMohammedShaikh: I am not completely sure what you mean by "we are safe until resources run out", but the rest of your comment seems correct. – CommonsWare May 20 '16 at 18:18
-
I meant we are safe about being notified for service being killed until resources run out thereby making it impossible to know whether the service was killed as the process itself is terminated without warning. Right? – Osama Mohammed Shaikh May 20 '16 at 18:24
-
@OsamaMohammedShaikh: Your process can also be killed without warning via Force Stop. For most devices, that's not a huge issue, as it's buried in your app's screen in Settings, and few users know about it. There are a handful of devices where the manufacturer appears to have screwed up and made their own "task manager" that does a force stop. This is *far* more likely to be used, and force stop has bigger ramifications than just having your process be terminated. – CommonsWare May 20 '16 at 18:33
-
So it seems there is no need to worry about being notified for service being killed as we will be notified in most cases excepting extreme resource constraint cases & force stops. Thank you for detailing & putting it in simple terms. I searched things up on google but could not get exact answer. – Osama Mohammed Shaikh May 20 '16 at 18:43