Situations when a Service's onDestroy() method doesn't get called?
Asked Answered
R

2

9

I'm aware that a Service's onDestroy() method may never be called but can someone tell me when such a scenario might occur? I'm especially interested in whether it's possible for a Service to be killed, yet its VM would continue to run.

I ask because I have a service that registers ContentObservers in the service's onStartCommand() method and unregisters them onDestroy(). If the service's onDestroy() method was never called because the whole VM was killed (along with the observers it created,) that would be fine. But I'm wondering if it's possible for a service to "go away" without onDestroy() being called, while the observers it created would live on and continue to receive changes.

Rushy answered 3/1, 2013 at 21:50 Comment(1)
I know that some low quality task killer's have the ability of stopping services. It may not call onDestroy() method.Nevlin
P
11

I'm aware that a Service's onDestroy() method may never be called but can someone tell me when such a scenario might occur?

Here are three off the top of my head:

  • If the user Force Stops you from the Settings app

  • If Android needs RAM in a hurry (e.g., to process an incoming phone call) and elects to terminate your process to free up that RAM

  • You terminate the process from DDMS

Also, if your service crashes with an unhandled exception somewhere, Android may consider the service to be defunct and skip onDestroy(). I'm not sure about this one, as I haven't specifically tried it.

But I'm wondering if it's possible for a service to "go away" without onDestroy() being called, while the observers it created would live on and continue to receive changes.

Other than the unhandled-exception possibility I mention above, I am reasonably certain that if the process will be terminated in the conditions where onDestroy() is not called.

Plains answered 3/1, 2013 at 21:57 Comment(3)
So it's safe to set a boolean member in the Application class named something like isMyServiceRunning default to false, and updated in Service's onCreate() and onDestroy() methods as if the Service is destroyed without onDestroy() called, then all the process, including the Application class is destroyed, right?Patiencepatient
@LouisCAD: "if the Service is destroyed without onDestroy() called, then all the process, including the Application class is destroyed, right?" -- that would not be the case necessarily for the unhandled exception scenario. For the scenarios in my bulleted list, though, you are correct.Plains
In my case, I have an UncaucghtException handler which schedule an app restart for some seconds later using AlarmManager, and then kills the process, making the unhandled exception case behave like the scenarios in your bulleted listPatiencepatient
M
2

Also if the app is reinstalled/updated , ondestroy() is never called.

Manikin answered 14/2, 2014 at 21:55 Comment(2)
Thanks for adding this information more than a year later for completion!Diagenesis
But you can listen to ACTION_MY_PACKAGE_REPLACED with a manifest Broadcast Receiver to make your app aware of this, and restore it's state if needed.Patiencepatient

© 2022 - 2024 — McMap. All rights reserved.