Skip to content

Most visited

Recently visited

navigation
added in version 1.0.0
belongs to Maven artifact android.arch.lifecycle:extensions:1.0.0

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

interface ViewModelProvider.Factory

Implementations of Factory interface are responsible to instantiate ViewModels. 

class ViewModelProvider.NewInstanceFactory

Simple factory, which calls empty constructor on the give class. 

Public constructors

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.

ViewModelProvider(ViewModelStore store, ViewModelProvider.Factory factory)

Creates ViewModelProvider, which will create ViewModels via the given Factory and retain them in the given store.

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 ViewModelProvider.

<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 ViewModelProvider.

Inherited methods

From class java.lang.Object

Public constructors

ViewModelProvider

added in version 1.0.0
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: a ViewModelStoreOwner whose ViewModelStore will be used to retain ViewModels

factory ViewModelProvider.Factory: a Factory which will be used to instantiate new ViewModels

ViewModelProvider

added in version 1.0.0
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: ViewModelStore where ViewModels will be stored.

factory ViewModelProvider.Factory: factory a Factory which will be used to instantiate new ViewModels

Public methods

get

added in version 1.0.0
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

added in version 1.0.0
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.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)