When a service is unbound from all clients, the Android system destroys it (unless it was also started withonStartCommand()
). As such, you don't have to manage the lifecycle of your service if it's purely a bound service—the Android system manages it for you based on whether it is bound to any clients.
However, if you choose to implement theonStartCommand()
callback method, then you must explicitly stop the service, because the service is now considered to bestarted. In this case, the service runs until the service stops itself withstopSelf()
or another component callsstopService()
, regardless of whether it is bound to any clients.
Additionally, if your service is started and accepts binding, then when the system calls youronUnbind()
method, you can optionally returntrue
if you would like to receive a call toonRebind()
the next time a client binds to the service.onRebind()
returns void, but the client still receives theIBinder
in itsonServiceConnected()
callback. The following figure illustrates the logic for this kind of lifecycle.