NavGraphViewModelLazyKt

Added in 2.1.0

public final class NavGraphViewModelLazyKt


Summary

Public methods

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

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

static final @NonNull Lazy<@NonNull VM>
@MainThread
<VM extends ViewModel> navGraphViewModels(
    @NonNull Fragment receiver,
    @NonNull String navGraphRoute,
    Function0<@NonNull CreationExtras> extrasProducer,
    Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

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

Public methods

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

Returns a property delegate to access a ViewModel scoped to a navigation graph present on the 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

Function0<@NonNull CreationExtras> extrasProducer

lambda that will be called during initialization to return CreationExtras. If none is provided, this will use the extras from the NavBackStackEntry referenced by the navGraphId.

Function0<@NonNull ViewModelProvider.Factory> factoryProducer

lambda that will be called during initialization to return ViewModelProvider.Factory. If none is provided, this will use the factory from the NavBackStackEntry referenced by the navGraphId.

@MainThread
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel> navGraphViewModels(
    @NonNull Fragment receiver,
    @NonNull String navGraphRoute,
    Function0<@NonNull CreationExtras> extrasProducer,
    Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

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

class MyFragment : Fragment() {
val viewModel: MainViewModel by navGraphViewModels("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("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
@NonNull String navGraphRoute

NavDestination.route of a NavGraph that exists on the NavController back stack. If a NavDestination with the given route does not exist on the back stack, an IllegalArgumentException will be thrown.

Function0<@NonNull CreationExtras> extrasProducer

lambda that will be called during initialization to return CreationExtras. If none is provided, this will use the extras from the NavBackStackEntry referenced by the navGraphRoute.

Function0<@NonNull ViewModelProvider.Factory> factoryProducer

lambda that will be called during initialization to return ViewModelProvider.Factory. If none is provided, this will use the factory from the NavBackStackEntry referenced by the navGraphRoute.