FragmentViewModelLazyKt

public final class FragmentViewModelLazyKt


Summary

Public methods

static final @MainThread @NonNull Lazy<@NonNull VM>
<VM extends ViewModel> FragmentViewModelLazyKt.activityViewModels(
    @NonNull Fragment receiver,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

Returns a property delegate to access parent activity's ViewModel, if factoryProducer is specified then ViewModelProvider.Factory returned by it will be used to create ViewModel first time.

static final @MainThread @NonNull Lazy<@NonNull VM>
<VM extends ViewModel> FragmentViewModelLazyKt.createViewModelLazy(
    @NonNull Fragment receiver,
    @NonNull KClass<@NonNull VM> viewModelClass,
    @NonNull Function0<@NonNull ViewModelStore> storeProducer,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

Helper method for creation of ViewModelLazy, that resolves null passed as factoryProducer to default factory.

static final @MainThread @NonNull Lazy<@NonNull VM>
<VM extends ViewModel> FragmentViewModelLazyKt.viewModels(
    @NonNull Fragment receiver,
    @NonNull Function0<@NonNull ViewModelStoreOwner> ownerProducer,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

Returns a property delegate to access ViewModel by default scoped to this Fragment:

Public methods

FragmentViewModelLazyKt.activityViewModels

@MainThread
@NonNull
public static final @MainThread Lazy<@NonNull VM> <VM extends ViewModel> FragmentViewModelLazyKt.activityViewModels(
    @NonNull Fragment receiver,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

Returns a property delegate to access parent activity's ViewModel, if factoryProducer is specified then ViewModelProvider.Factory returned by it will be used to create ViewModel first time. Otherwise, the activity's default factory will be used.

class MyFragment : Fragment() {
val viewmodel: MyViewModel by activityViewModels()
}

This property can be accessed only after this Fragment is attached i.e., after Fragment.onAttach(), and access prior to that will result in IllegalArgumentException.

FragmentViewModelLazyKt.createViewModelLazy

@MainThread
@NonNull
public static final @MainThread Lazy<@NonNull VM> <VM extends ViewModel> FragmentViewModelLazyKt.createViewModelLazy(
    @NonNull Fragment receiver,
    @NonNull KClass<@NonNull VM> viewModelClass,
    @NonNull Function0<@NonNull ViewModelStore> storeProducer,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

Helper method for creation of ViewModelLazy, that resolves null passed as factoryProducer to default factory.

FragmentViewModelLazyKt.viewModels

@MainThread
@NonNull
public static final @MainThread Lazy<@NonNull VM> <VM extends ViewModel> FragmentViewModelLazyKt.viewModels(
    @NonNull Fragment receiver,
    @NonNull Function0<@NonNull ViewModelStoreOwner> ownerProducer,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

Returns a property delegate to access ViewModel by default scoped to this Fragment:

class MyFragment : Fragment() {
val viewmodel: MyViewModel by viewmodels()
}

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

class MyFragment : Fragment() {
val viewmodel: MyViewModel by viewmodels { myFactory }
}

Default scope may be overridden with parameter ownerProducer:

class MyFragment : Fragment() {
val viewmodel: MyViewModel by viewmodels ({requireParentFragment()})
}

This property can be accessed only after this Fragment is attached i.e., after Fragment.onAttach(), and access prior to that will result in IllegalArgumentException.