CreationExtras
public abstract class CreationExtras
CreationExtras.Empty |
An empty read-only |
MutableCreationExtras |
A modifiable |
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 CreationExtrasAn empty read-only |
public interface CreationExtras.Key<T extends Object>Key for the elements of |
Public methods |
|
|---|---|
static final @NonNull CreationExtras.Key<@NonNull T> |
Returns an unique |
boolean |
Compares the specified object with this |
abstract T |
<T extends Object> get(@NonNull CreationExtras.Key<@NonNull T> key)Returns the value to which the specified |
int |
hashCode()Returns the hash code value for this |
@NonNull String |
toString()Returns a string representation of this |
Extension functions |
|
|---|---|
final boolean |
CreationExtrasKt.contains(Checks if the |
final @NonNull MutableCreationExtras |
CreationExtrasKt.plus(Creates a new read-only |
final @NonNull SavedStateHandle |
Creates |
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
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.
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 |