Skip to content
tezvyn:

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

Source: developer.android.comHardHow cards are made

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's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

An Activity binds to a service using BIND_AUTO_CREATE without startService. What happens to the service when the Activity is destroyed during rotation?

  • a.The service continues running until the new Activity instance calls unbindService on the old connection
  • b.The service survives because BIND_AUTO_CREATE persists the connection across the config change
  • c.The service is destroyed if it has no other bound clients, and the new Activity must bind againCorrect
  • d.The service is paused and resumes automatically when the new Activity restores its ServiceConnection
Why?

The binding is torn down when the Activity is destroyed during rotation, so the service is destroyed if it has no other clients or started state, and the new Activity must rebind. Option B is a common misconception because BIND_AUTO_CREATE does not persist or migrate connections across config changes.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles