SavedStateViewModelFactory



A ViewModelProvider.Factory that creates ViewModels with access to a SavedStateHandle in their constructor.

Summary

Public constructors

Constructs this factory.

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

Constructs a new SavedStateViewModelFactory.

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

Constructs a new SavedStateViewModelFactory.

android

Public functions

open T
<T : ViewModel> create(modelClass: Class<T>)

{@inheritDoc}

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

Creates a new instance of the given Class.

android
open T
<T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras)

{@inheritDoc}

android
open T
<T : ViewModel> create(modelClass: KClass<T>, extras: CreationExtras)

Creates a new instance of the given modelClass.

Cmn
android

Public constructors

SavedStateViewModelFactory

SavedStateViewModelFactory()

Constructs this factory.

When using this factory, the component scoping the SavedStateHandle must first invoke enableSavedStateHandles.

SavedStateViewModelFactory

SavedStateViewModelFactory(
    application: Application?,
    owner: SavedStateRegistryOwner
)

Constructs a new SavedStateViewModelFactory.

Parameters
application: Application?

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

owner: SavedStateRegistryOwner

SavedStateRegistryOwner that will provide restored state for created ViewModels. Must implement ViewModelStoreOwner to support state retention.

SavedStateViewModelFactory

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

Constructs a new SavedStateViewModelFactory.

When constructed this way, any CreationExtras provided to create will override the state configured here. It is not possible to mix the arguments received here with CreationExtras.

Parameters
application: Application?

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

owner: SavedStateRegistryOwner

SavedStateRegistryOwner that will provide restored state for created ViewModels. Must implement ViewModelStoreOwner to support state retention.

defaultArgs: Bundle?

default values to populate the SavedStateHandle if no state is restored

Throws
IllegalArgumentException

if the owner does not implement ViewModelStoreOwner

Public functions

create

open fun <T : ViewModel> create(modelClass: Class<T>): T

{@inheritDoc}

Throws
IllegalArgumentException

if the given modelClass does not have a classname

create

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

Creates a new instance of the given Class.

Parameters
key: String

a key associated with the requested ViewModel

modelClass: Class<T>

Class of the ViewModel to create

Returns
T

new ViewModel instance of type T

Throws
UnsupportedOperationException

if there is no lifecycle

create

open fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T

{@inheritDoc}

Throws
IllegalStateException

if the provided extras do not provide a ViewModelProvider.NewInstanceFactory.VIEW_MODEL_KEY

create

open fun <T : ViewModel> create(modelClass: KClass<T>, extras: CreationExtras): T

Creates a new instance of the given modelClass.

Parameters
modelClass: KClass<T>

KClass of the ViewModel to create

extras: CreationExtras

CreationExtras passed to the Factory to create the ViewModel

Returns
T

new ViewModel instance of type T