AbstractSavedStateViewModelFactory

abstract class AbstractSavedStateViewModelFactory : ViewModelProvider.Factory


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.

AbstractSavedStateViewModelFactory(
    owner: SavedStateRegistryOwner,
    defaultArgs: Bundle?
)

Constructs this factory.

Public functions

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

Creates a new instance of the given Class.

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

Creates a new instance of the given Class.

Protected functions

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

Creates a new instance of the given Class.

Public constructors

AbstractSavedStateViewModelFactory

Added in 2.5.0
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

Added in 1.0.0
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

Added in 1.0.0
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

Added in 2.5.0
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

Added in 1.0.0
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