AbstractSavedStateViewModelFactory



Skeleton of androidx.lifecycle.ViewModelProvider.KeyedFactory that creates SavedStateHandle for every requested ViewModel. The subclasses implement create to actually instantiate androidx.lifecycle.ViewModels.

Summary

Public constructors

Constructs this factory.

android
AbstractSavedStateViewModelFactory(
    owner: SavedStateRegistryOwner,
    defaultArgs: Bundle?
)

Constructs this factory.

android

Public functions

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

Creates a new instance of the given Class.

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

Creates a new instance of the given Class.

android

Protected functions

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

Creates a new instance of the given Class.

android

Inherited functions

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

Public constructors

AbstractSavedStateViewModelFactory

AbstractSavedStateViewModelFactory()

Constructs this factory.

When a factory is constructed this way, a component for which SavedStateHandle is scoped must have called enableSavedStateHandles. See CreationExtras.createSavedStateHandle docs for more details.

AbstractSavedStateViewModelFactory

AbstractSavedStateViewModelFactory(
    owner: SavedStateRegistryOwner,
    defaultArgs: Bundle?
)

Constructs this factory.

Parameters
owner: SavedStateRegistryOwner

SavedStateRegistryOwner that will provide restored state for created ViewModels

defaultArgs: Bundle?

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

Public functions

create

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

Creates a new instance of the given Class.

Parameters
modelClass: Class<T>

a Class whose instance is requested

Returns
T

a newly created ViewModel

Throws
kotlin.IllegalArgumentException

if the given modelClass is local or anonymous class.

kotlin.UnsupportedOperationException

if AbstractSavedStateViewModelFactory constructed with empty constructor, therefore no SavedStateRegistryOwner available for lifecycle

create

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

Creates a new instance of the given Class.

Parameters
modelClass: Class<T>

a Class whose instance is requested

extras: CreationExtras

an additional information for this creation request

Returns
T

a newly created ViewModel

Throws
kotlin.IllegalStateException

if no VIEW_MODEL_KEY provided by ViewModelProvider

Protected functions

create

protected abstract fun <T : ViewModel> create(
    key: String,
    modelClass: Class<T>,
    handle: SavedStateHandle
): T

Creates a new instance of the given Class.

Parameters
key: String

a key associated with the requested ViewModel

modelClass: Class<T>

a Class whose instance is requested

handle: SavedStateHandle

a handle to saved state associated with the requested ViewModel

Returns
T

the newly created ViewModel