Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent an Android service from getting destroyed after an unbind

I have a scenario where an activity starts a service by invoking the startService method: both the Activity and the Service are in the same package. Then the service, according to its configuration parameters, could launch an activity (Let's call it ExternalActivity) contained in a different package: this activity bind the service through bindService; once this activity has finished its tasks, it calls the unbindService method as follows...

// method of ExternalActivity
@Override
public void onDestroy() {
    super.onDestroy();
    unbindService(...);
}

As a consequence, the service is also destroyed. Is there the possibility of avoiding the destruction of the service?

like image 423
enzom83 Avatar asked Apr 29 '12 08:04

enzom83


1 Answers

As a consequence, the service is also destroyed.

As yorkw explained, a service is destroyed only when both of the following are true:

  1. All calls to bindService() have been matched by corresponding calls to unbindService().

  2. If somebody called startService(), somebody also called stopService() or the service called stopSelf().

Is there the possibility of avoiding the destruction of the service?

Find a better time to call stopService() or stopSelf(), whichever of those you are using.

like image 141
CommonsWare Avatar answered Sep 29 '22 08:09

CommonsWare