Marko Novakovic
03/18/2021, 1:49 PMclass OnActiveTriggerLiveData<T>(
private val doOnActive: MutableLiveData<T>.() -> Unit
) : MutableLiveData<T>() {
override fun onActive() {
super.onActive()
doOnActive()
}
}
and you have instance of this inside your ViewModel
like so:
class AccountViewModel(val repo: Repo) : ViewModel() {
private val _account = OnActiveTriggerLiveData<Account> { value = repo.account }
val account: LiveData<Account>
get() = _account
So every time view(Activity/Fragment) hits onStart
account will be updated with the newest value so you don’t have to expose method from ViewModel
and call it from onStart
nor onResume
You just observe LiveData
and not care about anything elseAdam Powell
03/18/2021, 2:56 PMMarko Novakovic
03/18/2021, 4:28 PM