0

How can I call multiple remote services from an activity? It seems that you can only have one onServiceConnected() in an activity? Can onServiceConnected() connect to multiple remote services at the same time?

  • Binding to a service directly from an activity has been an anti-pattern for a decade, as it does not deal with configuration changes. Use a repository, viewmodel, retained fragment, or something else that survives configuration changes. Beyond that, though, you can create different `ServiceConnection` objects, one per service. – CommonsWare Jul 26 '21 at 11:55
  • Sorry I don't understand. What configuration changes? Can different ServiceConnection objects connect to one onServiceConnected() and how do I do this? Android Studio does not allow two onServiceConnected() in the one Activity – stackuser99 Jul 27 '21 at 08:49
  • "What configuration changes?" -- see [the documentation on configuration changes](https://developer.android.com/guide/topics/resources/runtime-changes) to learn what they are. "Android Studio does not allow two onServiceConnected() in the one Activity" -- yes, it does. `ServiceConnection` is a Java interface. You can create more than one instance of `ServiceConnection`, just as you can create more than one instance of `View.OnClickListener`. – CommonsWare Jul 27 '21 at 10:36

0 Answers0