Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to call Service.startService() from within Service.onBind()?

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.

like image 502
Mark Avatar asked Feb 10 '13 18:02

Mark


Video Answer


1 Answers

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.

like image 53
David Wasser Avatar answered Sep 27 '22 18:09

David Wasser