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
Modifier and TypeClassDescriptionstatic interface
Implementations ofFactory
interface are responsible to instantiate ViewModels.static class
Simple factory, which calls empty constructor on the give class. -
Constructor Summary
ConstructorDescriptionCreatesViewModelProvider
.ViewModelProvider
(ViewModelStoreOwner owner, ViewModelProvider.Factory factory) CreatesViewModelProvider
, which will createViewModels
via the givenFactory
and retain them in a store of the givenViewModelStoreOwner
.ViewModelProvider
(ViewModelStore store, ViewModelProvider.Factory factory) CreatesViewModelProvider
, which will createViewModels
via the givenFactory
and 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 createViewModels
and retain them in a store of the givenViewModelStoreOwner
.This method will use the
default factory
if it returns not null. Otherwise, aViewModelProvider.NewInstanceFactory
will be used. -
ViewModelProvider
public ViewModelProvider(@Nonnull ViewModelStoreOwner owner, @Nonnull ViewModelProvider.Factory factory) CreatesViewModelProvider
, which will createViewModels
via the givenFactory
and retain them in a store of the givenViewModelStoreOwner
.- Parameters:
owner
- aViewModelStoreOwner
whoseViewModelStore
will be used to retainViewModels
factory
- aFactory
which will be used to instantiate newViewModels
-
ViewModelProvider
CreatesViewModelProvider
, which will createViewModels
via the givenFactory
and retain them in the givenstore
.- Parameters:
store
-ViewModelStore
where ViewModels will be stored.factory
- factory aFactory
which 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
.
-