Gabor Varadi
1 min readDec 26, 2019

--

Absolutely legit question!

I was focusing so much on LiveData.onActive() triggering the initial data load, that I overlooked that option (despite having gone with that approach previously myself).

Supposedly the key difference is that if you do something NetworkBoundResource-like, or at least something that tracks “if you should be downloading right now”, then you generally have a “try to check if we need more data”-like trigger in onStart. In our actual case, this was both onStart and onHiddenChanged().

So by moving the data load trigger to onActive(), that’s essentially equivalent to “attempting to start a data load triggered by onStart()”.

If you don’t need to “re-trigger” this fetch on back navigation for example, then yes, the initial load (or initial subscription to a websocket, etc) can definitely be done inside the init { of a ViewModel (mirrored by its onCleared()).

--

--

Gabor Varadi
Gabor Varadi

Written by Gabor Varadi

Android dev. Zhuinden, or EpicPandaForce @ SO. Extension function fan #Kotlin, dislikes multiple Activities/Fragment backstack.

No responses yet