Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onUnbind not being called when binding the activity to a running service

I have this activity that starts and binds to a service:

@Override
protected void onStart() {
    super.onStart();

    Intent intent = new Intent(context, SoundService.class);
    context.startService(intent);
    context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}

and I unbind by:

@Override
protected void onStop() {
    context.unbindService(serviceConnection);

    super.onStop();
}

The service keeps running even after closing the activity. Look at this scenario:

  1. Activity starts the service and binds to it
  2. Activity gets killed, the service keeps running, onUnbind() is called
  3. Activity starts again, and binds to the running service
  4. Activity gets killed, onUnbind() is not called :(

Why is the onUnbind() not being called?

like image 916
expGuy Avatar asked Feb 07 '23 19:02

expGuy


2 Answers

return true from onUnbind, next time if you call bindService again not only onRebind will be called but also when the last clients unbinds also onUnbind will be called by the system

like image 61
pskink Avatar answered Feb 11 '23 01:02

pskink


How did you start the service ? Please paste that code.

Service will only be stopped after you called unbindService() IF there are no other clients connected to this service AND the service was started with bind call with AUTO_CREATE. If the service was started with startService() then the service will not be stopped until the service calls stopSelf() or android terminates it to free memory

like image 36
RocketRandom Avatar answered Feb 11 '23 00:02

RocketRandom