Package icyllis.modernui.lifecycle

# Class ViewModelProvider

java.lang.Object
icyllis.modernui.lifecycle.ViewModelProvider

public class ViewModelProvider extends Object
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 Classes
  Modifier and Type
  Class
  Description
  `static interface`
  `ViewModelProvider.Factory`
  Implementations of `Factory` interface are responsible to instantiate ViewModels.
  `static class`
  `ViewModelProvider.NewInstanceFactory`
  Simple factory, which calls empty constructor on the give class.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ViewModelProvider(ViewModelStoreOwner owner)`
  Creates `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`.
  `ViewModelProvider(ViewModelStore store, ViewModelProvider.Factory factory)`
  Creates `ViewModelProvider`, which will create `ViewModels` via the given `Factory` and retain them in the given `store`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `<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`.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ViewModelProvider

    public ViewModelProvider(@Nonnull ViewModelStoreOwner owner)
    Creates `ViewModelProvider`. This will create `ViewModels` and retain them in a store of the given `ViewModelStoreOwner`.

    This method will use the `default factory` if it returns not null. Otherwise, a `ViewModelProvider.NewInstanceFactory` will be used.

  - ### ViewModelProvider

    public ViewModelProvider(@Nonnull ViewModelStoreOwner owner, @Nonnull 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` - a `ViewModelStoreOwner` whose `ViewModelStore` will be used to retain `ViewModels`
      `factory` - a `Factory` which will be used to instantiate new `ViewModels`

  - ### ViewModelProvider

    public ViewModelProvider(@Nonnull ViewModelStore store, @Nonnull ViewModelProvider.Factory factory)
    Creates `ViewModelProvider`, which will create `ViewModels` via the given `Factory` and retain them in the given `store`.
    Parameters:
      `store` - `ViewModelStore` where ViewModels will be stored.
      `factory` - factory a `Factory` which will be used to instantiate new `ViewModels`

- ## Method Details

  - ### get

    @Nonnull @UiThread public <T extends ViewModel> T get(@Nonnull 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).

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

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