I have a service which I want to make sure does NOT stop() when there are no longer any activities bound to it.
I understand that startService() is used to achieve this, but is it okay to call this from within the service's onBind() method?
Calling it from the activity seems like a hack to me, because the service know's best whether it wants to hang around after onUnbind().
The activities indicate the current state of the service and provide some control, so bindService() seems like the appropriate method to use here.
It should be fine to call startService()
from within onBind()
. If you have logic in onBind()
that can determine that the service needs to stay up even if the client has unbound then that seems to be the logical place to do it.
I disagree with the answer that claims it is not recommended. The architecture you've described is making effective use of encapsulation. Clients only want to bind/unbind and they shouldn't have to know how the service is (internally) implemented. The service should control its own lifecycle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With