SavedStateViewModelFactory

public final class SavedStateViewModelFactory
extends Object implements ViewModelProvider.Factory

java.lang.Object
   ↳ androidx.lifecycle.SavedStateViewModelFactory


ViewModelProvider.Factory that can create ViewModels accessing and contributing to a saved state via SavedStateHandle received in a constructor. If defaultArgs bundle was passed into the constructor, it will provide default values in SavedStateHandle.

If ViewModel is instance of AndroidViewModel, it looks for a constructor that receives an Application and SavedStateHandle (in this order), otherwise it looks for a constructor that receives SavedStateHandle only.

Summary

Public constructors

SavedStateViewModelFactory(Application application, SavedStateRegistryOwner owner)

Creates SavedStateViewModelFactory.

SavedStateViewModelFactory(Application application, SavedStateRegistryOwner owner, Bundle defaultArgs)

Creates SavedStateViewModelFactory.

Public methods

<T extends ViewModel> T create(String key, Class<T> modelClass)

Creates a new instance of the given Class.

<T extends ViewModel> T create(Class<T> modelClass)

Creates a new instance of the given Class.

Inherited methods

Public constructors

SavedStateViewModelFactory

public SavedStateViewModelFactory (Application application, 
                SavedStateRegistryOwner owner)

Creates SavedStateViewModelFactory.

ViewModel created with this factory can access to saved state scoped to the given activity.

Parameters
application Application: an application

owner SavedStateRegistryOwner: SavedStateRegistryOwner that will provide restored state for created ViewModels

SavedStateViewModelFactory

public SavedStateViewModelFactory (Application application, 
                SavedStateRegistryOwner owner, 
                Bundle defaultArgs)

Creates SavedStateViewModelFactory.

ViewModel created with this factory can access to saved state scoped to the given activity.

Parameters
application Application: an application

owner SavedStateRegistryOwner: SavedStateRegistryOwner that will provide restored state for created ViewModels

defaultArgs Bundle: values from this Bundle will be used as defaults by SavedStateHandle if there is no previously saved state or previously saved state misses a value by such key.

Public methods

create

public T create (String key, 
                Class<T> modelClass)

Creates a new instance of the given Class.

Parameters
key String: a key associated with the requested ViewModel

modelClass Class: a Class whose instance is requested

Returns
T a newly created ViewModel

create

public T create (Class<T> modelClass)

Creates a new instance of the given Class.

Parameters
modelClass Class: a Class whose instance is requested

Returns
T a newly created ViewModel