Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Navigation using LiveEvent subscription
sample:
https://github.com/alexshr/android-kotlin-fundamentals-apps/tree/master/MarsRealEstateFinal
---------------------------------------------
//https://github.com/hadilq/LiveEvent
implementation "com.github.hadilq.liveevent:liveevent:1.0.1"
-----------------------------
ViewModel:
/**
* navigation LiveEvent
* (see https://github.com/hadilq/LiveEvent)
* This is private because we don't want to expose setting this value to the Fragment.
*/
private val _navigateToSelectedProperty = LiveEvent<MarsProperty>()
val navigateToSelectedProperty: LiveData<MarsProperty> = _navigateToSelectedProperty
----------------------------
Fragment.onCreate:
// Observe the navigateToSelectedProperty LiveData and Navigate when it isn't null
// it was moved from onCreateView to avoid re-subscribing
viewModel.navigateToSelectedProperty.observe(this, Observer {
findNavController().navigate(OverviewFragmentDirections.actionShowDetail(it))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment