45

I have the Activity and the Service. When Activity is started, it calls startService() to make this Service be alive even when Activity is destroyed, and bindService(), to communicate with this Service.

bindService() returns true, mService.onBind() is called, and ServiceConnection.onServiceConnected() is called too. All works.

When i destroy my Activity by pressing Back key, it calls unbindService(), and my mService.onUnbind() is called. (i return false in this onUnbind().)

Then i start this Activity again, bindService() returns true, and then mService.onBind() is NOT called! But ServiceConnection.onServiceConnected() is called too, and all works again.

It looks like Dalvik remembers what my onBind() returned last time, and just does not call onBind() again. When i destroy my Activity again, onUnbind() is NOT called too.

I can bind and unbind this Service to my Activity any number of times, but these methods will not be called anymore until I destroy Service by unbinding and calling stopService().

In docs i can't find any explanation of this behavior. Conversely, this figure shows that onBind() and onUnbind() should be called every time clients bind and unbind Service. This figure can be found on the bottom of this Dev Guide.

0

3 Answers 3

60

I think this (referenced from official dev guide) can explain all your queries:

Multiple clients can connect to the service at once. However, the system calls your service's onBind() method to retrieve the IBinder only when the first client binds. The system then delivers the same IBinder to any additional clients that bind, without calling onBind() again.

5
  • 26
    oh thanks. I missed this, unfortunately. However, i found out that i can return true in onUnbind(), and then onRebind() and onUnbind() will be called for next client. Jan 10, 2012 at 4:52
  • 23
    The documentation is actually misleading. It says that no more calls to onBind() are done, but this applies only to the case of multiple clients. Docs also say that "if your service is started and accepts binding, then when the system calls your onUnbind() method, you can optionally return true if you would like to receive a call to onRebind() the next time a client binds to the service (instead of receiving a call to onBind())". Only there's no call to onBind(). Yet another case when documentation is wrong.
    – Malcolm
    Apr 8, 2012 at 17:17
  • sorry for bumping in @Malcolm... i have a question regarding that Binding and unBinding stuff. WHat about if I put a ServerSocket Thread inside that Service, and in Activity-A i put onStart() and onStop() with binding and unbinding accordingly. Would it be recreated if I move to another activity and binding it again? or will the Thread still remain the last one?
    – gumuruh
    Aug 20, 2014 at 12:49
  • @gumuruh Sorry for taking time to answer. According to Android documentation on services, the service will not be recreated if you use onStart and onStop to do the binding and unbinding. Maybe you should open a separate question for this (if doesn't exist yet, of course).
    – Malcolm
    Sep 1, 2014 at 0:46
  • Also important to note: onBind() is called multiple times in cases where clients send Intents with a different action while calling bindService(). Different extras doesn't seem to matter, though.
    – Leviathan
    Nov 27, 2020 at 19:04
1

You can find it here: https://developer.android.com/guide/components/bound-services.html

You can connect multiple clients to a service simultaneously. However, the system caches the IBinder service communication channel. In other words, the system calls the service's onBind() method to generate the IBinder only when the first client binds. The system then delivers that same IBinder to all additional clients that bind to that same service, without calling onBind() again.

0

You can unbind all the clients, then the service will be destroyed, and then the onBind will be called, if this is what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.