When a service's onDestroy is called due to low memory, in the method, should I do something like telling the worker threads that they should end their work and then wait them to exit before letting the system kill the hosting process? If so, how much time the system would allow me to wait? Should I make the worker threads capable of end themselves as fast as possible at anytime?
Or am I completely wrong?
Or maybe I should ask:
Where is the safest or best point to clean up the worker threads? Or what is the safest or best way to do so. Or there is no such thing at all, and a thread should be prepared to be killed without notice at anytime?
IIRC the lifecycle methods will only let you wait 1-2 seconds before showing an 'activity not responding' message. You can't, in general, wait for anything in these methods.
onDestroy
is the last lifecycle method, and is not called when the app is merely in the background. Do you want your threads to run in the background? It depends on your app but that's usually bad behavior.
I would suggest that you might want to pause in onPause()
which happens when the app is no longer in the foreground, or at least, in onStop()
when the app is no longer visible.