Back to Mavericks

Jetpack Navigation

docs/jetpack-navigation.md

3.1.01.4 KB
Original Source

Jetpack Navigation

Mavericks ships with a mavericks-navigation artifact that makes it easy to use nav graph scoped ViewModels.

Getting Started

Add the dependency to your build.gradle file:

groovy
dependencies {
  implementation 'com.airbnb.android:mavericks-navigation:x.y.z'
}

The latest version of mavericks-navigation is

Initialize Mavericks

In addition to the other setup steps, when you initialize Mavericks, you will need to pass in a custom viewModelDelegateFactory. To do that, replace:

kotlin
Mavericks.initialize(this)
// or 
MockableMavericks.initialize(this)

with:

kotlin
Mavericks.initialize(this, viewModelDelegateFactory = DefaultNavigationViewModelDelegateFactory())
// or 
MockableMavericks.initialize(this, viewModelDelegateFactory = DefaultNavigationViewModelDelegateFactory())

Usage

Once you have done that, you may use the navGraphViewModel() delegate to get a ViewModel scoped to the navigation graph with that id.

To view an example of its usage, check out the sample here.