Is there a way I can test if there are any other activities in my app still alive? I am looking to stop a service in an onDestroy method, but only want to do this if there
How can I launch a service that will continue to run as long as any of my app's activities are still on the stack, yet be stopped when none remain?
Don't use startService()
and stopService()
. Instead, use bindService()
and unbindService()
, even if you don't really need the bound connection. The bind/unbind is reference counted. If you call bindService()
with the BIND_AUTO_CREATE
flag, the first bindService()
call will start your service. Then, when all bindService()
calls have had matching unbindService()
calls, Android will automatically shut down your service.