How to inject ViewModel in service using hilt dagger

Viewed 295

I want to inject View Model in Service , how can it possible.

I recently started using DI in my project through Dagger Hilt. I'm having no problems when injecting viewModel instances into Activity/Fragment. But I am not sure how to inject this in service

1 Answers

The ViewModel should be used closely with an Activity or a Fragment, so it's destined to live in the UI layer of your application. Therefore, I don't recommend using the ViewModel in a Service. Create a different class, that would be used in the Service and, if needed, in the ViewModel. Like this you ensure the separation of concerns and avoid giving the ViewModel more responsibilities than needed.

https://github.com/android/architecture-components-samples/issues/137#issuecomment-327854042

Related