Package icyllis.modernui.lifecycle
Class ViewModelProvider
java.lang.Object
icyllis.modernui.lifecycle.ViewModelProvider
A utility class that provides
ViewModels for a scope.
Default ViewModelProvider for a Fragment can be obtained
by passing it to ViewModelProvider(ViewModelStoreOwner).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceImplementations ofFactoryinterface are responsible to instantiate ViewModels.static classSimple factory, which calls empty constructor on the give class. -
Constructor Summary
ConstructorsConstructorDescriptionCreatesViewModelProvider.ViewModelProvider(ViewModelStoreOwner owner, ViewModelProvider.Factory factory) CreatesViewModelProvider, which will createViewModelsvia the givenFactoryand retain them in a store of the givenViewModelStoreOwner.ViewModelProvider(ViewModelStore store, ViewModelProvider.Factory factory) CreatesViewModelProvider, which will createViewModelsvia the givenFactoryand retain them in the givenstore. -
Method Summary
Modifier and TypeMethodDescription<T extends ViewModel>
TReturns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with thisViewModelProvider.<T extends ViewModel>
TReturns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with thisViewModelProvider.
-
Constructor Details
-
ViewModelProvider
CreatesViewModelProvider. This will createViewModelsand retain them in a store of the givenViewModelStoreOwner.This method will use the
default factoryif it returns not null. Otherwise, aViewModelProvider.NewInstanceFactorywill be used. -
ViewModelProvider
public ViewModelProvider(@Nonnull ViewModelStoreOwner owner, @Nonnull ViewModelProvider.Factory factory) CreatesViewModelProvider, which will createViewModelsvia the givenFactoryand retain them in a store of the givenViewModelStoreOwner.- Parameters:
owner- aViewModelStoreOwnerwhoseViewModelStorewill be used to retainViewModelsfactory- aFactorywhich will be used to instantiate newViewModels
-
ViewModelProvider
CreatesViewModelProvider, which will createViewModelsvia the givenFactoryand retain them in the givenstore.- Parameters:
store-ViewModelStorewhere ViewModels will be stored.factory- factory aFactorywhich will be used to instantiate newViewModels
-
-
Method Details
-
get
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with thisViewModelProvider.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).
- Type Parameters:
T- The type parameter for the ViewModel.- Parameters:
modelClass- The class of the ViewModel to create an instance of it if it is not present.- Returns:
- A ViewModel that is an instance of the given type
T.
-
get
@Nonnull @UiThread public <T extends ViewModel> T get(@Nonnull String key, @Nonnull Class<T> modelClass) Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with thisViewModelProvider.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).
- Type Parameters:
T- The type parameter for the ViewModel.- Parameters:
key- The key to use to identify the ViewModel.modelClass- The class of the ViewModel to create an instance of it if it is not present.- Returns:
- A ViewModel that is an instance of the given type
T.
-