ViewModelProvider
public final 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 |
|---|
public class ViewModelProvider.AndroidViewModelFactory extends ViewModelProvider.NewInstanceFactory
|
public static class ViewModelProvider.Companion |
public interface ViewModelProvider.FactoryImplementations of |
public class ViewModelProvider.NewInstanceFactory implements ViewModelProvider.FactorySimple factory, which calls empty constructor on the give class. |
public class ViewModelProvider.NewInstanceFactory implements ViewModelProvider.FactorySimple factory, which calls empty constructor on the give class. |
public static class ViewModelProvider.NewInstanceFactory.Companion |
Public constructors |
|---|
|
Creates |
ViewModelProvider(Creates a |
ViewModelProvider(Creates a |
Public methods |
|
|---|---|
@NonNull T |
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
final @NonNull T |
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
@NonNull T |
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
final @NonNull T |
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
Extension functions |
|
|---|---|
final @NonNull VM |
@MainThreadReturns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
final @NonNull VM |
@MainThreadReturns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this |
Public constructors
ViewModelProvider
public ViewModelProvider(@NonNull ViewModelStoreOwner owner)
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
public ViewModelProvider(
@NonNull ViewModelStoreOwner owner,
@NonNull ViewModelProvider.Factory factory
)
Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.
| Parameters | |
|---|---|
@NonNull ViewModelStoreOwner owner |
The |
@NonNull ViewModelProvider.Factory factory |
The |
ViewModelProvider
public ViewModelProvider(
@NonNull ViewModelStore store,
@NonNull ViewModelProvider.Factory factory,
@NonNull CreationExtras defaultCreationExtras
)
Creates a ViewModelProvider. This provider generates ViewModel instances using the specified Factory and stores them within the ViewModelStore of the provided ViewModelStoreOwner.
| Parameters | |
|---|---|
@NonNull ViewModelStore store |
|
@NonNull ViewModelProvider.Factory factory |
The |
@NonNull CreationExtras defaultCreationExtras |
Additional data to be passed to the |
Public methods
get
public @NonNull T <T extends ViewModel> get(@NonNull Class<@NonNull 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 | |
|---|---|
@NonNull Class<@NonNull T> modelClass |
The class of the ViewModel to create an instance of it if it is not present. |
| Returns | |
|---|---|
@NonNull T |
A ViewModel that is an instance of the given type |
| Throws | |
|---|---|
IllegalArgumentException |
if the given |
get
@MainThread
public final @NonNull T <T extends ViewModel> get(@NonNull KClass<@NonNull 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 | |
|---|---|
@NonNull KClass<@NonNull T> modelClass |
The class of the ViewModel to create an instance of it if it is not present. |
| Returns | |
|---|---|
@NonNull T |
A ViewModel that is an instance of the given type |
| Throws | |
|---|---|
IllegalArgumentException |
if the given |
get
public @NonNull T <T extends ViewModel> get(@NonNull String key, @NonNull Class<@NonNull 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 | |
|---|---|
@NonNull String key |
The key to use to identify the ViewModel. |
@NonNull Class<@NonNull T> modelClass |
The class of the ViewModel to create an instance of it if it is not present. |
| Returns | |
|---|---|
@NonNull T |
A ViewModel that is an instance of the given type |
get
@MainThread
public final @NonNull T <T extends ViewModel> get(@NonNull String key, @NonNull KClass<@NonNull 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 | |
|---|---|
@NonNull String key |
The key to use to identify the ViewModel. |
@NonNull KClass<@NonNull T> modelClass |
The class of the ViewModel to create an instance of it if it is not present. |
| Returns | |
|---|---|
@NonNull T |
A ViewModel that is an instance of the given type |
Extension functions
ViewModelProviderGetKt.get
@MainThread
public final @NonNull VM <VM extends ViewModel> ViewModelProviderGetKt.get(
@NonNull ViewModelProvider receiver
)
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) |
ViewModelProviderGetKt.get
@MainThread
public final @NonNull VM <VM extends ViewModel> ViewModelProviderGetKt.get(
@NonNull ViewModelProvider receiver,
@NonNull String key
)
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider and the given key.
| See also | |
|---|---|
get |
(String, Class) |