belongs to Maven artifact android.arch.lifecycle:viewmodel:1.1.1
ViewModelProvider
  public
  
  
  
  class
  ViewModelProvider
  
    extends Object
  
  
  
  
  
  
| java.lang.Object | |
| ↳ | android.arch.lifecycle.ViewModelProvider | 
An utility class that provides ViewModels for a scope.
 
 Default ViewModelProvider for an Activity or a Fragment can be obtained
 from ViewModelProviders class.
Summary
| Nested classes | |
|---|---|
| 
        
        
        
        
        class | ViewModelProvider.AndroidViewModelFactory
 | 
| 
        
        
        
        
        interface | ViewModelProvider.FactoryImplementations of  | 
| 
        
        
        
        
        class | ViewModelProvider.NewInstanceFactorySimple factory, which calls empty constructor on the give class. | 
| Public constructors | |
|---|---|
| 
      ViewModelProvider(ViewModelStoreOwner owner, ViewModelProvider.Factory factory)
      Creates  | |
| 
      ViewModelProvider(ViewModelStore store, ViewModelProvider.Factory factory)
      Creates  | |
| Public methods | |
|---|---|
| 
        
        
        
        
        <T extends ViewModel>
        T | 
      get(Class<T> modelClass)
      Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or
 an activity), associated with this  | 
| 
        
        
        
        
        <T extends ViewModel>
        T | 
      get(String key, Class<T> modelClass)
      Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or
 an activity), associated with this  | 
| Inherited methods | |
|---|---|
Public constructors
ViewModelProvider
ViewModelProvider (ViewModelStoreOwner owner, ViewModelProvider.Factory factory)
Creates ViewModelProvider, which will create ViewModels via the given
 Factory and retain them in a store of the given ViewModelStoreOwner.
| Parameters | |
|---|---|
| owner | ViewModelStoreOwner: aViewModelStoreOwnerwhoseViewModelStorewill be used to
                retainViewModels | 
| factory | ViewModelProvider.Factory: aFactorywhich will be used to instantiate
                newViewModels | 
ViewModelProvider
ViewModelProvider (ViewModelStore store, ViewModelProvider.Factory factory)
Creates ViewModelProvider, which will create ViewModels via the given
 Factory and retain them in the given store.
| Parameters | |
|---|---|
| store | ViewModelStore:ViewModelStorewhere ViewModels will be stored. | 
| factory | ViewModelProvider.Factory: factory aFactorywhich will be used to instantiate
                newViewModels | 
Public methods
get
T get (Class<T> modelClass)
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: 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 T. | 
get
T get (String key, 
                Class<T> modelClass)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: 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 T. | 
- Annotations
- Interfaces
- Classes- AndroidViewModel
- Lifecycle
- LifecycleRegistry
- LifecycleService
- LiveData
- LiveDataReactiveStreams
- MediatorLiveData
- MutableLiveData
- ProcessLifecycleOwner
- ServiceLifecycleDispatcher
- Transformations
- ViewModel
- ViewModelProvider
- ViewModelProvider.AndroidViewModelFactory
- ViewModelProvider.NewInstanceFactory
- ViewModelProviders
- ViewModelProviders.DefaultFactory
- ViewModelStore
- ViewModelStores
 
- Enums
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
