I've an host activity with 4 tabs, one fragment for each tab.
In the host activity I collect the user's location through
FusedLocationProviderClient
.I want to pass that location to the child fragments.
It seems that I've to use a ViewModel
like:
class LocationViewModel : ViewModel() {
lateinit var location: Location
}
How can I inform the fragments when the location
has been set by the host Activity?
Thanks.
EDIT - Solution
Using MutableLiveData
as suggested did the trick.
class LocationViewModel : ViewModel()
{
private val mutableLiveLocation = MutableLiveData<Location>()
val liveLocation: LiveData<Location> get() = mutableLiveLocation
fun setLocation(location: Location)
{
mutableLiveLocation.value = location
}
}