would you say this is good practice? ```class OnAc...
# android
m
would you say this is good practice?
Copy code
class 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:
Copy code
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 else
a
Yes, this is the principle behind, "cold observables" that powers both kotlinx.coroutines.flow and rxjava. Generally onActive should register some kind of callback to get ongoing updates rather than just update one time and onInactive should unregister that callback to stop. You should also extend LiveData instead of MutableLiveData so that the set operation is not exposed.
👌 1
m
@Adam Powell thank you very much