ViewModelScenarioKt

Added in 2.9.0-alpha06

public final class ViewModelScenarioKt


Summary

Public methods

static final @NonNull ViewModelScenario<@NonNull VM>
<VM extends ViewModel> viewModelScenario(
    @NonNull CreationExtras creationExtras,
    @ExtensionFunctionType @NonNull Function1<@NonNull CreationExtras, @NonNull VM> initializer
)

Creates a ViewModelScenario using a given VM class as key, an initializer as a ViewModelProvider.Factory and a creationExtras as default extras.

static final @NonNull ViewModelScenario<@NonNull VM>
<VM extends ViewModel> viewModelScenario(
    @NonNull ViewModelProvider.Factory factory,
    @NonNull CreationExtras creationExtras
)

Creates a ViewModelScenario using a given VM class as key, an factory and a creationExtras as default extras.

Public methods

public static final @NonNull ViewModelScenario<@NonNull VM> <VM extends ViewModel> viewModelScenario(
    @NonNull CreationExtras creationExtras,
    @ExtensionFunctionType @NonNull Function1<@NonNull CreationExtras, @NonNull VM> initializer
)

Creates a ViewModelScenario using a given VM class as key, an initializer as a ViewModelProvider.Factory and a creationExtras as default extras.

You should access the ViewModel instance using ViewModelScenario.viewModel, and clear the ViewModelStore using ViewModelScenario.close.

Example usage:

viewModelScenario { MyViewModel(parameters) }
.use { scenario ->
val vm = scenario.viewModel
// Use the ViewModel
}
Parameters
<VM extends ViewModel>

The reified ViewModel class to be created.

@NonNull CreationExtras creationExtras

Additional data passed to the Factory during a ViewModel creation.

@ExtensionFunctionType @NonNull Function1<@NonNull CreationExtras, @NonNull VM> initializer

A Factory function to create a ViewModel.

public static final @NonNull ViewModelScenario<@NonNull VM> <VM extends ViewModel> viewModelScenario(
    @NonNull ViewModelProvider.Factory factory,
    @NonNull CreationExtras creationExtras
)

Creates a ViewModelScenario using a given VM class as key, an factory and a creationExtras as default extras.

You should access the ViewModel instance using ViewModelScenario.viewModel, and clear the ViewModelStore using ViewModelScenario.close.

Example usage:

val myFactory: ViewModelProvider.Factory = MyViewModelFactory()
viewModelScenario<MyViewModel>(myFactory)
.use { scenario ->
val vm = scenario.viewModel
// Use the ViewModel
}
Parameters
<VM extends ViewModel>

The reified ViewModel class to be created.

@NonNull ViewModelProvider.Factory factory

A Factory to create a ViewModel.

@NonNull CreationExtras creationExtras

Additional data passed to the Factory during a ViewModel creation.