getOrCreateViewModel

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.

Parameters

owner

The fragment owning this store.

kotlinClass

The KClass information on the ViewModel being stored.

key

Unique identifier for this ViewModel.

factory

The factory to create a new ViewModel, when not found in the store.