FragmentViewModelLazyKt

Added in 1.1.0

public final class FragmentViewModelLazyKt


Summary

Public methods

static final @NonNull Lazy<@NonNull VM>
@MainThread
<VM extends ViewModel> activityViewModels(
    @NonNull Fragment receiver,
    Function0<@NonNull CreationExtras> extrasProducer,
    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 @NonNull Lazy<@NonNull VM>
@MainThread
<VM extends ViewModel> createViewModelLazy(
    @NonNull Fragment receiver,
    @NonNull KClass<@NonNull VM> viewModelClass,
    @NonNull Function0<@NonNull ViewModelStore> storeProducer,
    @NonNull Function0<@NonNull CreationExtras> extrasProducer,
    Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

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

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

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

Public methods

activityViewModels

@MainThread
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel> activityViewModels(
    @NonNull Fragment receiver,
    Function0<@NonNull CreationExtras> extrasProducer,
    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.

createViewModelLazy

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

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

This method also takes an CreationExtras produces that provides default extras to the created view model.

@MainThread
public static final @NonNull Lazy<@NonNull VM> <VM extends ViewModel> viewModels(
    @NonNull Fragment receiver,
    @NonNull Function0<@NonNull ViewModelStoreOwner> ownerProducer,
    Function0<@NonNull CreationExtras> extrasProducer,
    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.