tezvyn:

What happens to a bound service during config changes and multiple clients?

Curated by the Tezvyn teamSource: developer.android.comadvanced
What happens to a bound service during config changes and multiple clients?

Tests bound service reference counting. Config change destroys the Activity binding; if no other clients exist, the service stops. Multiple clients keep it alive until the last unbind. Red flag: assuming the service survives config changes without a rebind.

WHAT THIS TESTS: This question probes your deep knowledge of the bound service lifecycle and the internal reference counting that the Android system maintains for every ServiceConnection. It specifically checks whether you understand that BIND_AUTO_CREATE is strictly tied to the client lifecycle, that configuration changes are not magical persistence mechanisms, and that multiple bindings are refcounted rather than treated as independent kill switches. The interviewer wants to hear precise mechanics, not hand-waving about Android handling it automatically.

A GOOD ANSWER COVERS: First, explain that BIND_AUTO_CREATE creates the Service only if it is not already running, and that the binding itself acts as a lifecycle tether between the client and the service process. Second, state clearly that when an Activity is destroyed during a configuration change, the binding is torn down during teardown; if this was the sole active binding and the Service was not also started with startService, the Service is destroyed and its onDestroy callback runs. Third, clarify that the new Activity instance created after rotation must call bindService again in onCreate or onStart to establish a new connection and potentially recreate the Service. Fourth, describe that when multiple clients bind to the same service, the system increments an internal reference count, so the Service stays alive until the final client calls unbindService and the count drops to zero.

COMMON WRONG ANSWERS: A dangerous mistake is asserting that the Service survives a configuration change because BIND_AUTO_CREATE somehow persists the connection across the destroy and create cycle. Another red flag is saying that calling unbind from one client immediately stops the Service even when other clients are still bound, which reveals a misunderstanding of refcounting. Some candidates also conflate bound services with started services, claiming that onStartCommand keeps the Service alive independently of bindings without mentioning that the two lifecycle paths are completely separate unless you explicitly combine them in a hybrid service.

LIKELY FOLLOW-UPS: The interviewer may ask how to retain a bound service across configuration changes without the teardown, which opens the door to discussing a retained fragment, a ViewModel-backed approach, or binding from the Application context. They might also ask what happens if you forget to unbind and leak the Activity, or how to convert a bound service into a foreground service so it keeps running after all clients unbind. A sharp candidate should also be ready to discuss service discovery and the difference between binding to a local service versus a remote service in another process.

ONE CONCRETE EXAMPLE: Imagine a music playback Service bound by an Activity for transport controls. If the user rotates the device, the Activity is destroyed and recreated. Without a retained fragment or an Application-level binding, the Service is torn down during rotation and its onDestroy runs, only to be recreated when the new Activity calls bindService again in onCreate. Now imagine a second Activity in a multi-window scenario also binds to the same Service. The Service continues running when the first Activity unbinds, only stopping after the second Activity also calls unbindService and the system refcount reaches zero.

Source: developer.android.com

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

What happens to a bound service during config changes and multiple clients? · Tezvyn