ViewModelProviderWrapper

This object contains functions that assist in the creation of a ViewModelProvider and retrieving view models from it. This is the preferred method of interacting with ViewModelProvider as it facilitates the mocking of view model creation during view comparison tests.

Functions

Link copied to clipboard
fun <T : ViewModel> getOrCreateViewModel(owner: ViewModelStoreOwner, kotlinClass: KClass<*>, key: String? = null, factory: ViewModelProvider.Factory? = null): T

Returns an existing ViewModel from the ViewModelStore owned by the ViewModelStoreOwner. If no existing ViewModel is in the store it creates a new one using a factory provided in factory. If factory is null a factory will be retrieved by calling HasDefaultViewModelProviderFactory.defaultViewModelProviderFactory on the ViewModelStoreOwner or a default factory will be used expecting a parameter-less constructor for kotlinClass.

Link copied to clipboard
fun <T : ViewModel> getViewModelOrThrow(owner: ViewModelStoreOwner, kotlinClass: KClass<*>, key: String): T

Returns an existing ViewModel or throws an exception when no matching view model can be restored.