NavGraphViewModelLazyKt

public final class NavGraphViewModelLazyKt


Summary

Public methods

static final @MainThread @NonNull Lazy<@NonNull VM>
<VM extends ViewModel> NavGraphViewModelLazyKt.navGraphViewModels(
    @NonNull Fragment receiver,
    @IdRes int navGraphId,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

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

Public methods

NavGraphViewModelLazyKt.navGraphViewModels

@MainThread
@NonNull
public static final @MainThread Lazy<@NonNull VM> <VM extends ViewModel> NavGraphViewModelLazyKt.navGraphViewModels(
    @NonNull Fragment receiver,
    @IdRes int navGraphId,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

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

class MyFragment : Fragment() {
val viewmodel: MainViewModel by navGraphViewModels(R.id.main)
}

Custom ViewModelProvider.Factory can be defined via factoryProducer parameter, factory returned by it will be used to create ViewModel:

class MyFragment : Fragment() {
val viewmodel: MainViewModel by navGraphViewModels(R.id.main) { myFactory }
}

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
@IdRes int navGraphId

ID of a NavGraph that exists on the NavController back stack