CreationExtras


public abstract class CreationExtras

Known direct subclasses
CreationExtras.Empty

An empty read-only CreationExtras.

MutableCreationExtras

A modifiable CreationExtras that holds pairs of CreationExtras.Key and Any, allowing efficient value retrieval for each key.


A map-like object holding pairs of CreationExtras.Key and Any, enabling efficient value retrieval for each key. Each key in CreationExtras is unique, storing only one value per key.

CreationExtras is used in ViewModelProvider.Factory.create to provide extra information to the Factory. This makes Factory implementations stateless, simplifying factory injection by not requiring all information at construction time.

This abstract class supports read-only access; use MutableCreationExtras for read-write access.

Summary

Nested types

public static class CreationExtras.Empty extends CreationExtras

An empty read-only CreationExtras.

public interface CreationExtras.Key<T extends Object>

Key for the elements of CreationExtras.

Public methods

static final @NonNull CreationExtras.Key<@NonNull T>
<T extends Object> Key()

Returns an unique Key to be associated with an extra.

boolean
equals(Object other)

Compares the specified object with this CreationExtras for equality.

abstract T
<T extends Object> get(@NonNull CreationExtras.Key<@NonNull T> key)

Returns the value to which the specified key is associated, or null if this CreationExtras contains no mapping for the key.

int

Returns the hash code value for this CreationExtras.

@NonNull String

Returns a string representation of this CreationExtras.

Extension functions

final boolean

Checks if the CreationExtras contains the given key.

final @NonNull MutableCreationExtras
CreationExtrasKt.plus(
    @NonNull CreationExtras receiver,
    @NonNull CreationExtras creationExtras
)

Creates a new read-only CreationExtras by replacing or adding entries to this extras from another creationExtras.

final @NonNull SavedStateHandle

Creates SavedStateHandle that can be used in your ViewModels

Public methods

Key

public static final @NonNull CreationExtras.Key<@NonNull T> <T extends Object> Key()

Returns an unique Key to be associated with an extra.

equals

public boolean equals(Object other)

Compares the specified object with this CreationExtras for equality.

get

Added in 2.5.0
public abstract T <T extends Object> get(@NonNull CreationExtras.Key<@NonNull T> key)

Returns the value to which the specified key is associated, or null if this CreationExtras contains no mapping for the key.

hashCode

public int hashCode()

Returns the hash code value for this CreationExtras.

toString

public @NonNull String toString()

Returns a string representation of this CreationExtras. The string representation consists of a list of key-value mappings in the order returned by the CreationExtras's iterator.

Extension functions

CreationExtrasKt.contains

public final boolean CreationExtrasKt.contains(
    @NonNull CreationExtras receiver,
    @NonNull CreationExtras.Key<@NonNull ?> key
)

Checks if the CreationExtras contains the given key.

This method allows to use the key in creationExtras syntax for checking whether an key is contained in the CreationExtras.

CreationExtrasKt.plus

public final @NonNull MutableCreationExtras CreationExtrasKt.plus(
    @NonNull CreationExtras receiver,
    @NonNull CreationExtras creationExtras
)

Creates a new read-only CreationExtras by replacing or adding entries to this extras from another creationExtras.

The returned CreationExtras preserves the entry iteration order of the original CreationExtras.

Those entries of another creationExtras that are missing in this extras are iterated in the end in the order of that creationExtras.

SavedStateHandleSupport.createSavedStateHandle

@MainThread
public final @NonNull SavedStateHandle SavedStateHandleSupport.createSavedStateHandle(
    @NonNull CreationExtras receiver
)

Creates SavedStateHandle that can be used in your ViewModels

This function requires enableSavedStateHandles call during the component initialization. Latest versions of androidx components like ComponentActivity, Fragment, NavBackStackEntry makes this call automatically.

This CreationExtras must contain SAVED_STATE_REGISTRY_OWNER_KEY, VIEW_MODEL_STORE_OWNER_KEY and VIEW_MODEL_KEY.

Throws
kotlin.IllegalArgumentException

if this CreationExtras are missing required keys: ViewModelStoreOwnerKey, SavedStateRegistryOwnerKey, VIEW_MODEL_KEY