0

Can I safely perform bindService() and unbindService() when a button is clicked inside an Activity or Fragment, instead of binding in onStart() and unbinding in onStop()?

Will it cause any issue if the Activity is closed while the service is still bound this way?

btnBind.setOnClickListener {
    bindService(intent, connection, Context.BIND_AUTO_CREATE)
}

btnUnbind.setOnClickListener {
    unbindService(connection)
}

Is this approach okay, or should I always manage the binding inside lifecycle callbacks for safety?

5
  • IMHO, binding and unbinding of services does not belong in UI code at all. It belongs in a singleton repository, use case, or viewmodel. Commented May 26 at 13:01
  • Thanks for the suggestion — I agree that separating the binding logic into a manager or repository is cleaner. But in my case, I need to trigger the bind and unbind based on user button clicks directly in the UI, since the service needs to handle immediate actions triggered by the user. Is it still acceptable to bind and unbind temporarily from the UI for this kind of user-driven action, or would you recommend always delegating it through a manager and exposing the state back to the UI? @CommonsWare Commented May 26 at 14:27
  • I would recommend always delegating it, per your last sentence. The time needed to perform that delegation will be best measured in microseconds and will not impact the responsiveness of your app. The inter-process communication involved in the bound service will be much greater. You might also reconsider whether you need a bound service in the first place, if this is one that you created. Commented May 26 at 14:48
  • Thanks for the insights! I’m trying to decide between using a bound service or just a foreground service with broadcasts for my app’s wake up call feature. The app needs to update the UI when the alarm is accepted or canceled and show a timer while ringing. Would you say the complexity of bound service is worth it in this case, or does broadcast with foreground service handle it well enough? Also, are there any trade-offs I should consider? Appreciate your advice! Commented May 26 at 15:49
  • It is unclear why you have any service for this scenario, but there could well be features that require it. If the service runs in its own process, binding (so you can register a callback for those UI updates) feels like your best option, off the cuff. If the service is in the same process as the UI, then you definitely do not need binding, as it only adds value across process boundaries IMHO. Commented May 26 at 17:27

1 Answer 1

0

Key issues to watch out for :

Calling unbindService() without being bound If you call unbindService() when the service is not currently bound, Android will throw an IllegalArgumentException:

java.lang.IllegalArgumentException: Service not registered

Always track binding state using a flag:

var isBound = false

btnBind.setOnClickListener {
    if (!isBound) {
        bindService(intent, connection, Context.BIND_AUTO_CREATE)
        isBound = true
    }
}

btnUnbind.setOnClickListener {
    if (isBound) {
        unbindService(connection)
        isBound = false
    }
}

Activity destruction without unbinding If the user closes the Activity (via back press or system), and it was still bound to the service, Android will automatically unbind it only if the service was bound using Context.bindService() and the context is the Activity itself. But you should not rely on this. If you forget to unbind, it may cause memory leaks or other side effects.

Lifecycle mismatch Manual binding doesn't automatically follow the Activity lifecycle (like onStart()/onStop()), which can lead to inconsistency if the Activity is recreated (e.g., due to a config change).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.