Skip to content

Instantly share code, notes, and snippets.

@alexshr
Last active August 12, 2019 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexshr/a85b05608f1fefc9e50501fe61681366 to your computer and use it in GitHub Desktop.
Save alexshr/a85b05608f1fefc9e50501fe61681366 to your computer and use it in GitHub Desktop.
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