SavedStateViewModelFactory


public final class SavedStateViewModelFactory implements ViewModelProvider.Factory


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

Summary

Public constructors

Constructs this factory.

Constructs a new SavedStateViewModelFactory.

SavedStateViewModelFactory(
    Application application,
    @NonNull SavedStateRegistryOwner owner,
    Bundle defaultArgs
)

Constructs a new SavedStateViewModelFactory.

Public methods

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

{@inheritDoc}

final @NonNull T
<T extends ViewModel> create(
    @NonNull String key,
    @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
)

{@inheritDoc}

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

Creates a new instance of the given modelClass.

Public constructors

SavedStateViewModelFactory

Added in 2.5.0
public SavedStateViewModelFactory()

Constructs this factory.

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

SavedStateViewModelFactory

Added in 2.3.0
public SavedStateViewModelFactory(
    Application application,
    @NonNull SavedStateRegistryOwner owner
)

Constructs a new SavedStateViewModelFactory.

Parameters
Application application

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

@NonNull SavedStateRegistryOwner owner

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

SavedStateViewModelFactory

Added in 2.3.0
public SavedStateViewModelFactory(
    Application application,
    @NonNull SavedStateRegistryOwner owner,
    Bundle defaultArgs
)

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.

@NonNull SavedStateRegistryOwner owner

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

Bundle defaultArgs

default values to populate the SavedStateHandle if no state is restored

Throws
IllegalArgumentException

if the owner does not implement ViewModelStoreOwner

Public methods

create

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

{@inheritDoc}

Throws
IllegalArgumentException

if the given modelClass does not have a classname

create

Added in 2.3.0
public final @NonNull T <T extends ViewModel> create(
    @NonNull String key,
    @NonNull Class<@NonNull T> modelClass
)

Creates a new instance of the given Class.

Parameters
@NonNull String key

a key associated with the requested ViewModel

@NonNull Class<@NonNull T> modelClass

Class of the ViewModel to create

Returns
@NonNull T

new ViewModel instance of type T

Throws
UnsupportedOperationException

if there is no lifecycle

create

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

{@inheritDoc}

Throws
IllegalStateException

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

create

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

Creates a new instance of the given modelClass.

Parameters
@NonNull KClass<@NonNull T> modelClass

KClass of the ViewModel to create

@NonNull CreationExtras extras

CreationExtras passed to the Factory to create the ViewModel

Returns
@NonNull T

new ViewModel instance of type T