Do I need to call both unbindService and stopService for Android services?

A gotcha that I hit with this:

Ensure you call unbindService on the same context that you called bindService. In my case, I was doing the following to bind it:

Context c = getApplicationContext();
c.bindService(...);

Then to unbind it, just:

unbindService(...);

Making sure both bind and unbind used the same context solved the problem.


The Android documentation for stopService() states:

Note that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed. See the Service documentation for more details on a service's lifecycle.

So calling stopService() first followed by unbindService() should work (it's working for me).