Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Service: onBind(Intent) and onUnbind(Intent) is called just once

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.

like image 884
Dmitry Frank Avatar asked Jan 09 '12 12:01

Dmitry Frank


People also ask

What is the use of onBind () in Android?

To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.

What is onBind?

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. Follow this answer to receive notifications.

What is bound and unbound service in Android?

Bounded services are bounded to an activity which binds it and will work only till bounded activity is alive. while a unbounded service will work till the completion even after activity is destroyed.

Which method is used to stop service in Android?

You stop a service via the stopService() method. No matter how frequently you called the startService(intent) method, one call to the stopService() method stops the service. A service can terminate itself by calling the stopSelf() method.


1 Answers

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.

like image 120
yorkw Avatar answered Oct 19 '22 23:10

yorkw