SavedStateViewModelFactory



androidx.lifecycle.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 androidx.lifecycle.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. androidx.lifecycle.AndroidViewModel is only supported if you pass a non-null Application instance.

Summary

Public constructors

Constructs this factory.

android
SavedStateViewModelFactory(
    application: Application?,
    owner: SavedStateRegistryOwner
)

Creates SavedStateViewModelFactory.

android
SavedStateViewModelFactory(
    application: Application?,
    owner: SavedStateRegistryOwner,
    defaultArgs: Bundle?
)

Creates SavedStateViewModelFactory.

android

Inherited functions

From androidx.lifecycle.ViewModelProvider.Factory
open T
<T : ViewModel> create(modelClass: KClass<T>, extras: CreationExtras)
android

Public constructors

SavedStateViewModelFactory

SavedStateViewModelFactory()

Constructs this factory.

When a factory is constructed this way, a component for which SavedStateHandle is scoped must have called enableSavedStateHandles.

See also
createSavedStateHandle

docs for more details.

SavedStateViewModelFactory

SavedStateViewModelFactory(
    application: Application?,
    owner: SavedStateRegistryOwner
)

Creates SavedStateViewModelFactory.

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

Parameters
application: Application?

an application. If null, AndroidViewModel instances will not be supported.

owner: SavedStateRegistryOwner

SavedStateRegistryOwner that will provide restored state for created ViewModels

SavedStateViewModelFactory

SavedStateViewModelFactory(
    application: Application?,
    owner: SavedStateRegistryOwner,
    defaultArgs: Bundle?
)

Creates SavedStateViewModelFactory.

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

When a factory is constructed this way, if you add any CreationExtras those arguments will be used instead of the state passed in here. It is not possible to mix the arguments received here with the CreationExtras.

Parameters
application: Application?

an application. If null, AndroidViewModel instances will not be supported.

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.