ActivityViewModelLazyKt

public final class ActivityViewModelLazyKt


Summary

Public methods

static final @MainThread @NonNull Lazy<@NonNull VM>
<VM extends ViewModel> ActivityViewModelLazyKt.viewModels(
    @NonNull ComponentActivity receiver,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

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

Public methods

ActivityViewModelLazyKt.viewModels

@MainThread
@NonNull
public static final @MainThread Lazy<@NonNull VM> <VM extends ViewModel> ActivityViewModelLazyKt.viewModels(
    @NonNull ComponentActivity receiver,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

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

class MyComponentActivity : ComponentActivity() {
val viewmodel: MyViewModel by viewModels()
}

This property can be accessed only after the Activity is attached to the Application, and access prior to that will result in IllegalArgumentException.