Is an android service guaranteed to call onDestroy()?

I’m not sure where you’re seeing that a Service is guaranteed to have onDestroy() called. As far as I know, this isn’t the case. If you read this page of the docs, it describes the conditions in which a service could be killed. So if you’re asking if a process which hosts both an activity and service is being killed, will onDestroy() be called on the service (but not on the activity) then the answer is no; a service’s onDestroy() will not necessarily be called. As to whether a service-only process can be abruptly killed by the OS: yes, it can. This is especially true when you have a lot of work to do, and your onStartCommand call only queues up the work to do asynchronously. Then the service will be spending the majority of its time not in the protected onCreate, onStartCommand or onDestroy methods.

Leave a Comment