android-viewmodel

Android Architecture Components ViewModel - communication with Service/IntentService

混江龙づ霸主 提交于 2019-11-27 12:09:05
问题 I'm exploring Google's Android Architecture Components. In my project I'm relying on Services and IntentServices. What is the correct way to communicate with app's ViewModel from an IntentService or Service? Is it achievable using LiveData? 回答1: TL;DR It's achievable - use an observer relationship. Your IntentService and likely location service should not be aware of your ViewModel. Consider using a Repository. LiveData can be used (see postValue). It's good for updating the UI (ViewModel to

Manually clearing an Android ViewModel?

限于喜欢 提交于 2019-11-27 11:57:07
问题 With reference to the android.arch.lifecycle.ViewModel class. ViewModel is scoped to the lifecycle of the UI component it relates to, so in a Fragment -based app, that will be the fragment lifecycle. This is a good thing. In some cases one wants to share a ViewModel instance between multiple fragments. Specifically I am interested in the case where many screens relate to the same underlying data . (The docs suggest similar approach when multiple related fragments are displayed on the same