Android 12 Developer Preview is here! Try it out, and give us your feedback!

androidx.hilt.navigation.fragment

Extension functions summary

For Fragment
Lazy<VM>
Fragment.hiltNavGraphViewModels(@IdRes navGraphId: Int)

Returns a property delegate to access a HiltViewModel-annotated ViewModel scoped to a navigation graph present on the {@link NavController} back stack:

Extension functions

hiltNavGraphViewModels

@MainThread inline fun <reified VM : ViewModel> Fragment.hiltNavGraphViewModels(@IdRes navGraphId: Int): Lazy<VM>

Returns a property delegate to access a HiltViewModel-annotated ViewModel scoped to a navigation graph present on the {@link NavController} back stack:

class MyFragment : Fragment() {
    val viewmodel: MainViewModel by androidx.hilt.navigation.fragment.hiltNavGraphViewModels(R.navigation.main)
}

This property can be accessed only after this NavGraph is on the NavController back stack, and an attempt access prior to that will result in an IllegalArgumentException.

Parameters
navGraphId: Int ID of a NavGraph that exists on the {@link NavController} back stack