I noticed that one of my Android services is no longer single
Android Services is a single-user mode, regardless of whether you run it / bind it in a real working environment or when testing a toolkit. By singleton, I mean a unique object that lives on the heap, which can have multiple references to it.
Calling the startService or bindService function from the test set should result in the second onBind or onStartCommand being specified
this is not as stated in the official guide for developers: βSeveral clients can connect to the service at a time. However, the system calls your onBind () method to retrieve the IBinder only when the first client contacts. The system then delivers the same IBinder to any additional to clients that bind without calling onBind () again. "
but should never cause the second to be created before the first onDestroy
According to the official guide for developers: βIf you allow the launch and binding of your service, then when the service starts, the system does not destroy the service when all clients disconnect. Instead, you must explicitly stop the services by calling stopSelf () or stopService (). "
Thus, the scenario behind the scenes is that the first call to the start or bind service calls the Service.onCreate () method ((before calling onStartCommand () or onBind ()) to create a unique object on the heap and a reference to it is returned (counter references = 1), after that every time you call the start or binding, Service.onStartCommand () performs without creating a new object (by calling Service.onCreate ()) on the heap, instead return the second anchor point to the same object (now reference count = 2), every time you call unbind, the number of links decreases by one until the number of links reaches 0, the Service.onDestroy () method is called, and finally the object on the heap is cleared.
You can find all the details that I refer to in italics from the official developer guide here .
yorkw source share