I am re-using a my_layout.xml
with two different ViewModel & LiveData
in which function should I update my UI onCreate()
/onStart()
/onResume()
(& why??), so won't be redundant...
Set your Observer
s on your LiveData
in onCreate
(or something like onViewCreated
in a Fragment). Those observer functions should update the UI when a new value comes in, so you just write the function telling it how to update - display this value in this TextView
, that kind of thing).
Once you've wired that up, the UI will update itself as the LiveData
changes. And if those LiveData
s already have data, you'll get it as soon as you call observe
- so the update function will run and initialise your UI with the current data.
You just need to have references to your inflated views (like the TextView
you want to update) so you can use them in the update functions. That's why it has to be after you've called setContentView
in onCreate
, or in onViewCreated
in a fragment, where you have access to the inflated view
.
LiveData
(and any other lifecycle-aware components) will automatically update when you go from onStop
to onResume
etc, and your updates will just happen