NavGraphViewModelLazyKt

public final class NavGraphViewModelLazyKt


Summary

Public methods

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

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

static final @MainThread @NonNull Lazy<@NonNull VM>
<VM extends ViewModel> NavGraphViewModelLazyKt.navGraphViewModels(
    Fragment receiver,
    String navGraphRoute,
    Function0<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
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel> NavGraphViewModelLazyKt.navGraphViewModels(
    Fragment receiver,
    @IdRes int navGraphId,
    Function0<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

NavGraphViewModelLazyKt.navGraphViewModels

@MainThread
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel> NavGraphViewModelLazyKt.navGraphViewModels(
    Fragment receiver,
    String navGraphRoute,
    Function0<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("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.

P