30

I've started using Architecture Components in my application and I'm still learning how to use it.

In my app I have an Activity showing different Fragments sequentially. In some of them I need to communicate with a background service in order to receive data from external BLE sensors. Since I need to interact with the service in more than one Fragment, I'm wondering if ViewModel is the right place where to make the binding. I've looked around but I didn't found an answer.

Are there any problems binding a service inside a ViewModel?

hara
  • 3,274
  • 4
  • 37
  • 55
  • I'm wondering the same thing. It seems if I bind the service within the view model, the service is not guaranteed to be bound in time. However, if the service is bound within the activity, that's just doesn't make sense to me. Did you figure this out? – masterwok Feb 20 '18 at 05:46
  • 5
    Hi masterwok. I'm currently using a binging inside a ViewModel in my application. Everything seems to work fine. I read the post mentioned by @Igor Bubelov while I was looking for a solution but I didn't find a better approach and to me it seems the least worst option having a binding in the ViewModel. Waiting to see if other arguments arise – hara Feb 20 '18 at 10:15
  • 1
    I have been binding my service to the ViewModel, and it works very well. The ViewModel is the only thing with the exact same lifecycle as my service. I don't like doing it, since it seems to contravene The Rule of Thumb mentioned here, but I don't know what else to bind it to. My repository and try to think of the service as a data source? I wish Google would provide some more clarity about its new architecture. – John Jun 28 '18 at 13:32
  • What do you think about binding the service inside fragments instead of viewmodels? – blow Apr 03 '21 at 06:46
  • @blow Depends on application design and what needs access to the service. If only a single fragment needs access do it there, else do it at the activity or view model level. – denver Apr 05 '21 at 17:50

1 Answers1

16

It is not advisable to use Android framework classes inside ViewModels.

Here is the link on Google Developers blog post with the detailed explanation: ViewModels and LiveData: Patterns + AntiPatterns

Ideally, ViewModels shouldn’t know anything about Android. This improves testability, leak safety and modularity. A general rule of thumb is to make sure there are no android.* imports in your ViewModels (with exceptions like android.arch.*). The same applies to presenters.

Don’t let ViewModels (and Presenters) know about Android framework classes

Igor Bubelov
  • 5,154
  • 5
  • 25
  • 24
  • 1
    For testability you want your ViewModels independent from the Android framework, but regardless, you end up with some code tied to the OS service management that is difficult to test. In the last part of the linked post, on Extending LiveData they mention handling of services with LiveData and ViewModels, so clearly they don't rule it out if that is the appropriate level to interface with the service at. – denver Apr 05 '21 at 17:55