Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

SavedStateVMFactory

public final class SavedStateVMFactory
extends AbstractSavedStateVMFactory

java.lang.Object
   ↳ androidx.lifecycle.AbstractSavedStateVMFactory
     ↳ androidx.lifecycle.SavedStateVMFactory


ViewModelProvider.Factory that can create ViewModels accessing and contributing to a saved state via SavedStateHandle received in a constructor. If defaultArgs bundle was passed in SavedStateVMFactory(Fragment, Bundle) or SavedStateVMFactory(FragmentActivity, Bundle), 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

SavedStateVMFactory(Fragment fragment)

Creates SavedStateVMFactory.

SavedStateVMFactory(Fragment fragment, Bundle defaultArgs)

Creates SavedStateVMFactory.

SavedStateVMFactory(FragmentActivity activity)

Creates SavedStateVMFactory.

SavedStateVMFactory(FragmentActivity activity, Bundle defaultArgs)

Creates SavedStateVMFactory.

SavedStateVMFactory(Application application, SavedStateRegistryOwner owner, Bundle defaultArgs)

Creates SavedStateVMFactory.

Protected methods

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

Creates a new instance of the given Class.

Inherited methods

Public constructors

SavedStateVMFactory

public SavedStateVMFactory (Fragment fragment)

Creates SavedStateVMFactory.

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

Parameters
fragment Fragment: scope of this fragment will be used for state saving

SavedStateVMFactory

public SavedStateVMFactory (Fragment fragment, 
                Bundle defaultArgs)

Creates SavedStateVMFactory.

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

Parameters
fragment Fragment: scope of this fragment will be used for state saving

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

SavedStateVMFactory

public SavedStateVMFactory (FragmentActivity activity)

Creates SavedStateVMFactory.

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

Parameters
activity FragmentActivity: scope of this activity will be used for state saving

SavedStateVMFactory

public SavedStateVMFactory (FragmentActivity activity, 
                Bundle defaultArgs)

Creates SavedStateVMFactory.

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

Parameters
activity FragmentActivity: scope of this activity will be used for state saving

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.

SavedStateVMFactory

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

Creates SavedStateVMFactory.

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.

Protected methods

create

protected T create (String key, 
                Class<T> modelClass, 
                SavedStateHandle handle)

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

handle SavedStateHandle: a handle to saved state associated with the requested ViewModel

Returns
T a newly created ViewModels