# HiltNavGraphViewModelLazyKt

Added in 1.0.0

*** ** * ** ***


```
public final class HiltNavGraphViewModelLazyKt
```

<br />

*** ** * ** ***

## Summary

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

## Public methods

### 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
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel> hiltNavGraphViewModels(
    @NonNull Fragment receiver,
    @IdRes int navGraphId
)
```

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

```
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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IdRes int navGraphId` | ID of a NavGraph that exists on the `https://developer.android.com/reference/androidx/navigation/NavController` back stack |

### 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
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel, VMF extends Object> hiltNavGraphViewModels(
    @NonNull Fragment receiver,
    @IdRes int navGraphId,
    @NonNull Function1<@NonNull VMF, @NonNull VM> creationCallback
)
```

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

```
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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IdRes int navGraphId` | ID of a NavGraph that exists on the `https://developer.android.com/reference/androidx/navigation/NavController` back stack |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull VMF, @https://developer.android.com/reference/androidx/annotation/NonNull VM> creationCallback` | callback that takes an @AssistedFactory-annotated factory and creates a HiltViewModel using @AssistedInject-annotated constructor. |