ViewModelProvider.NewInstanceFactory



Factory that creates ViewModel instances by calling their no-argument constructor.

Summary

Public companion properties

CreationExtras.Key<String>

A CreationExtras.Key used to retrieve the key associated with a requested ViewModel.

android

Public constructors

android

Public functions

open T
<T : ViewModel> create(modelClass: Class<T>)

Creates a new instance of the given Class.

android
open T
<T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras)

Creates a new instance of the given Class.

android
open T
<T : ViewModel> create(modelClass: KClass<T>, extras: CreationExtras)

Creates a new instance of the given modelClass.

android

Public companion properties

VIEW_MODEL_KEY

val VIEW_MODEL_KEYCreationExtras.Key<String>

A CreationExtras.Key used to retrieve the key associated with a requested ViewModel.

The ViewModelProvider automatically includes the key in the CreationExtras passed to ViewModelProvider.Factory. This applies to keys generated by either of these usage patterns:

  • ViewModelProvider.get(key, MyViewModel::class): provided key is used.

  • ViewModelProvider.get(MyViewModel::class): generates a key from given class.

See also
VIEW_MODEL_KEY

Public constructors

NewInstanceFactory

NewInstanceFactory()

Public functions

create

open fun <T : ViewModel> create(modelClass: Class<T>): T

Creates a new instance of the given Class.

Default implementation throws UnsupportedOperationException.

Parameters
modelClass: Class<T>

Class of the ViewModel to create

Returns
T

new ViewModel instance of type T

create

open fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T

Creates a new instance of the given Class.

Parameters
modelClass: Class<T>

Class of the ViewModel to create

extras: CreationExtras

CreationExtras passed to the Factory to create the ViewModel

Returns
T

new ViewModel instance of type T

create

open fun <T : ViewModel> create(modelClass: KClass<T>, extras: CreationExtras): T

Creates a new instance of the given modelClass.

Parameters
modelClass: KClass<T>

KClass of the ViewModel to create

extras: CreationExtras

CreationExtras passed to the Factory to create the ViewModel

Returns
T

new ViewModel instance of type T