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

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