ViewModelProvider
-
Cmn
class ViewModelProvider
-
android
open class ViewModelProvider
A utility class that provides ViewModels for a scope.
Default ViewModelProvider for an Activity or a Fragment can be obtained by passing it to the constructor: ViewModelProvider(myFragment)
Summary
Nested types |
|---|
|
|
interface ViewModelProvider.FactoryImplementations of |
|
Simple factory, which calls empty constructor on the give class. |
|
Simple factory, which calls empty constructor on the give class. |
Public companion functions |
||
|---|---|---|
ViewModelProvider |
create(Creates a |
Cmn
android
N
|
ViewModelProvider |
create(Creates a |
Cmn
android
N
|
Public companion properties |
||
|---|---|---|
CreationExtras.Key<String> |
A |
Cmn
android
N
|
Public constructors |
|
|---|---|
ViewModelProvider(owner: ViewModelStoreOwner)Creates |
android
|
ViewModelProvider(Creates a |
android
|
ViewModelProvider(Creates a |
android
|
Public functions |
||
|---|---|---|
open operator T |
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
android
|
operator T |
@MainThreadReturns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
Cmn
android
N
|
open operator T |
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
android
|
operator T |
@MainThreadReturns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
Cmn
android
N
|
Extension functions |
||
|---|---|---|
inline VM |
@MainThreadReturns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
Cmn
|
Public companion functions
create
fun create(
owner: ViewModelStoreOwner,
factory: ViewModelProvider.Factory = ViewModelProviders.getDefaultFactory(owner),
extras: CreationExtras = ViewModelProviders.getDefaultCreationExtras(owner)
): ViewModelProvider
Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.
| Parameters | |
|---|---|
owner: ViewModelStoreOwner |
The |
factory: ViewModelProvider.Factory = ViewModelProviders.getDefaultFactory(owner) |
The |
extras: CreationExtras = ViewModelProviders.getDefaultCreationExtras(owner) |
Additional data to be passed to the |
create
fun create(
store: ViewModelStore,
factory: ViewModelProvider.Factory = DefaultViewModelProviderFactory,
extras: CreationExtras = CreationExtras.Empty
): ViewModelProvider
Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.
| Parameters | |
|---|---|
store: ViewModelStore |
|
factory: ViewModelProvider.Factory = DefaultViewModelProviderFactory |
factory a |
extras: CreationExtras = CreationExtras.Empty |
Additional data to be passed to the |
Public companion properties
VIEW_MODEL_KEY
val VIEW_MODEL_KEY: CreationExtras.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): providedkeyis used. -
ViewModelProvider.get(MyViewModel::class): generates akeyfrom givenclass.
Public constructors
ViewModelProvider
ViewModelProvider(owner: ViewModelStoreOwner)
Creates ViewModelProvider. This will create ViewModel instances and retain them in the ViewModelStore of the given ViewModelStoreOwner.
This method will use the default factory if the owner implements HasDefaultViewModelProviderFactory. Otherwise, a NewInstanceFactory will be used.
ViewModelProvider
ViewModelProvider(
owner: ViewModelStoreOwner,
factory: ViewModelProvider.Factory
)
Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.
| Parameters | |
|---|---|
owner: ViewModelStoreOwner |
The |
factory: ViewModelProvider.Factory |
The |
ViewModelProvider
ViewModelProvider(
store: ViewModelStore,
factory: ViewModelProvider.Factory,
defaultCreationExtras: CreationExtras = CreationExtras.Empty
)
Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.
| Parameters | |
|---|---|
store: ViewModelStore |
|
factory: ViewModelProvider.Factory |
The |
defaultCreationExtras: CreationExtras = CreationExtras.Empty |
Additional data to be passed to the |
Public functions
get
open operator fun <T : ViewModel> get(modelClass: Class<T>): T
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider.
The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed).
| Parameters | |
|---|---|
modelClass: Class<T> |
The class of the ViewModel to create an instance of it if it is not present. |
| Returns | |
|---|---|
T |
A ViewModel that is an instance of the given type |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given |
get
@MainThread
operator fun <T : ViewModel> get(modelClass: KClass<T>): T
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider.
The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed).
| Parameters | |
|---|---|
modelClass: KClass<T> |
The class of the ViewModel to create an instance of it if it is not present. |
| Returns | |
|---|---|
T |
A ViewModel that is an instance of the given type |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given |
get
open operator fun <T : ViewModel> get(key: String, modelClass: Class<T>): T
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider.
The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed).
| Parameters | |
|---|---|
key: String |
The key to use to identify the ViewModel. |
modelClass: Class<T> |
The class of the ViewModel to create an instance of it if it is not present. |
| Returns | |
|---|---|
T |
A ViewModel that is an instance of the given type |
get
@MainThread
operator fun <T : ViewModel> get(key: String, modelClass: KClass<T>): T
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider.
The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed).
| Parameters | |
|---|---|
key: String |
The key to use to identify the ViewModel. |
modelClass: KClass<T> |
The class of the ViewModel to create an instance of it if it is not present. |
| Returns | |
|---|---|
T |
A ViewModel that is an instance of the given type |
Extension functions
get
@MainThread
inline fun <VM : ViewModel> ViewModelProvider.get(): VM
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider.
| See also | |
|---|---|
get |
(Class) |