# androidx.hilt.navigation.fragment

*** ** * ** ***

## Extension functions summary

|---|---|
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy/index.html<VM>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <VM : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel> https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment.https://developer.android.com/reference/kotlin/androidx/hilt/navigation/fragment/package-summary#(androidx.fragment.app.Fragment).hiltNavGraphViewModels(kotlin.Int)(navGraphId: @https://developer.android.com/reference/kotlin/androidx/annotation/IdRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns a property delegate to access a [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel) -annotated `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` scoped to a navigation graph present on the `https://developer.android.com/reference/kotlin/androidx/navigation/NavController` back stack: |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy/index.html<VM>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <VM : https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel, VMF : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment.https://developer.android.com/reference/kotlin/androidx/hilt/navigation/fragment/package-summary#(androidx.fragment.app.Fragment).hiltNavGraphViewModels(kotlin.Int,kotlin.Function1)( navGraphId: @https://developer.android.com/reference/kotlin/androidx/annotation/IdRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, noinline creationCallback: (VMF) -> VM )` Returns a property delegate to access a [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel) -annotated `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` with an @AssistedInject-annotated constructor that is scoped to a navigation graph present on the `https://developer.android.com/reference/kotlin/androidx/navigation/NavController` back stack: |

## Extension functions

### Fragment.hiltNavGraphViewModels

Artifact: [androidx.hilt:hilt-navigation-fragment](https://developer.android.com/jetpack/androidx/releases/hilt) [View Source](https://cs.android.com/search?q=file:androidx/hilt/navigation/fragment/HiltNavGraphViewModelLazy.kt+function:hiltNavGraphViewModels)

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

Returns a property delegate to access a [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel) -annotated `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` scoped to a navigation graph present on the `https://developer.android.com/reference/kotlin/androidx/navigation/NavController` back stack:

```kotlin
class MyFragment : Fragment() {
    val viewmodel: MainViewModel by 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: @https://developer.android.com/reference/kotlin/androidx/annotation/IdRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | ID of a NavGraph that exists on the `https://developer.android.com/reference/kotlin/androidx/navigation/NavController` back stack |

### Fragment.hiltNavGraphViewModels

Artifact: [androidx.hilt:hilt-navigation-fragment](https://developer.android.com/jetpack/androidx/releases/hilt) [View Source](https://cs.android.com/search?q=file:androidx/hilt/navigation/fragment/HiltNavGraphViewModelLazy.kt+function:hiltNavGraphViewModels)

```
@MainThread
inline fun <VM : ViewModel, VMF : Any> Fragment.hiltNavGraphViewModels(
    navGraphId: @IdRes Int,
    noinline creationCallback: (VMF) -> VM
): Lazy<VM>
```

Returns a property delegate to access a [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel) -annotated `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` with an @AssistedInject-annotated constructor that is scoped to a navigation graph present on the `https://developer.android.com/reference/kotlin/androidx/navigation/NavController` back stack:

```kotlin
class MyFragment : Fragment() {
    val viewmodel: MainViewModel by hiltNavGraphViewModels(R.navigation.main) { factory: MainViewModelFactory ->
        factory.create(...)
    }
}
```

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: @https://developer.android.com/reference/kotlin/androidx/annotation/IdRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | ID of a NavGraph that exists on the `https://developer.android.com/reference/kotlin/androidx/navigation/NavController` back stack |
| `noinline creationCallback: (VMF) -> VM` | callback that takes an @AssistedFactory-annotated factory and creates a HiltViewModel using @AssistedInject-annotated constructor. |