AbstractSavedStateViewModelFactory


public abstract class AbstractSavedStateViewModelFactory implements 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.

Constructs this factory.

Public methods

@NonNull T
<T extends ViewModel> create(@NonNull Class<@NonNull T> modelClass)

Creates a new instance of the given Class.

@NonNull T
<T extends ViewModel> create(
    @NonNull Class<@NonNull T> modelClass,
    @NonNull CreationExtras extras
)

Creates a new instance of the given Class.

Protected methods

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

Creates a new instance of the given Class.

Inherited methods

From androidx.lifecycle.ViewModelProvider.Factory
@NonNull T
<T extends ViewModel> create(
    @NonNull KClass<@NonNull T> modelClass,
    @NonNull CreationExtras extras
)

Public constructors

AbstractSavedStateViewModelFactory

Added in 2.5.0
public 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
public AbstractSavedStateViewModelFactory(
    @NonNull SavedStateRegistryOwner owner,
    Bundle defaultArgs
)

Constructs this factory.

Parameters
@NonNull SavedStateRegistryOwner owner

SavedStateRegistryOwner that will provide restored state for created ViewModels

Bundle defaultArgs

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 methods

create

Added in 1.0.0
public @NonNull T <T extends ViewModel> create(@NonNull Class<@NonNull T> modelClass)

Creates a new instance of the given Class.

Parameters
@NonNull Class<@NonNull T> modelClass

a Class whose instance is requested

Returns
@NonNull 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
public @NonNull T <T extends ViewModel> create(
    @NonNull Class<@NonNull T> modelClass,
    @NonNull CreationExtras extras
)

Creates a new instance of the given Class.

Parameters
@NonNull Class<@NonNull T> modelClass

a Class whose instance is requested

@NonNull CreationExtras extras

an additional information for this creation request

Returns
@NonNull T

a newly created ViewModel

Throws
kotlin.IllegalStateException

if no VIEW_MODEL_KEY provided by ViewModelProvider

Protected methods

create

Added in 1.0.0
protected abstract @NonNull T <T extends ViewModel> create(
    @NonNull String key,
    @NonNull Class<@NonNull T> modelClass,
    @NonNull SavedStateHandle handle
)

Creates a new instance of the given Class.

Parameters
@NonNull String key

a key associated with the requested ViewModel

@NonNull Class<@NonNull T> modelClass

a Class whose instance is requested

@NonNull SavedStateHandle handle

a handle to saved state associated with the requested ViewModel

Returns
@NonNull T

the newly created ViewModel