[Question] Using ViewModels in Service
See original GitHub issueHi,
Everything is great with ViewModel
. They work as expected in the Activity
and Fragment
. Recently I have encountered an need to use the ViewModel
in Service
.
I’m not sure how exactly to do that.
I found one answer on StackOverFlow: https://stackoverflow.com/questions/44708202/observe-livedata-from-foreground-service
But I want to know what’s the best practice to use ViewModel
in Service
.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:17
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Access viewModel inside a service in android - Stack Overflow
The ViewModel should be used closely with an Activity or a Fragment, so it's destined to live in the UI layer of your...
Read more >Services and ViewModels in Android MVVM - YouTube
Android : Services and ViewModels in Android MVVM - How do they interact?
Read more >ASP.NET MVC use ViewModels with Services
I'm trying to make a simple Blog-application, but I'm a bit stuck on the architecture of the application. I'd like to create a...
Read more >ViewModel overview - Android Developers
ViewModel lets you manage your UI's data in a lifecycle-aware fashion.
Read more >Android View Model and How it works internally - DeHaat
ViewModel can be created with activity context or fragment context. When a ViewModel object is created, it is stored inside Activity OR ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
How your Service negotiate with the UI?