# SavedStateContainer

Artifact: [androidx.savedstate:savedstate](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt) [View Source](https://cs.android.com/search?q=file:androidx/savedstate/SavedStateContainer.kt+class:androidx.savedstate.SavedStateContainer) Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

*** ** * ** ***


```
public final class SavedStateContainer
```

<br />

*** ** * ** ***

A container that manages `SavedStateValue` instances to coordinate state saving and restoration across hierarchical and key-value based components.

`SavedStateContainer` allows registering, retrieving, removing, and nesting state-saving components. When saving or restoring state, operations are delegated across all registered `SavedStateValue` instances.

## Summary

| ### Public constructors |
|---|
| `SavedStateContainer()` |

| ### Public methods |
|---|---|
| `final boolean` | `contains(@NonNull String key)` Checks if a `SavedStateValue` is registered under the given `key`. |
| `final @NonNull SavedStateContainer` | `getOrCreateContainer(@NonNull String key)` Retrieves an existing nested `SavedStateContainer` associated with `key`, or creates and registers a new child `SavedStateContainer` if none exists. |
| `final @NonNull R` | `<T extends Object, R extends SavedStateValue<@NonNull T>> getOrPutSavedStateValue( @NonNull String key, @NonNull Function0<@NonNull R> defaultValue )` Returns an existing `SavedStateValue` registered for `key`, or evaluates `defaultValue`, registers the result in this container, and returns it. |
| `final R` | `<T extends Object, R extends SavedStateValue<@NonNull T>> getSavedStateValue( @NonNull String key )` Retrieves the registered `SavedStateValue` associated with `key`. |
| `final @NonNull Set<@NonNull String>` | `keys()` Returns a `Set` containing all keys registered in this `SavedStateContainer`. |
| `final void` | `<T extends Object, R extends SavedStateValue<@NonNull T>> putSavedStateValue( @NonNull String key, @NonNull R savedStateValue )` Registers a `savedStateValue` for state saving and restoration under the given `key`. |
| `final R` | `<T extends Object, R extends SavedStateValue<@NonNull T>> removeSavedStateValue( @NonNull String key )` Removes and returns the registered `SavedStateValue` associated with `key`. |
| `final void` | `restoreState(SavedState savedState)` Restores state for all registered `SavedStateValue` instances using the provided `savedState`. |
| `final @NonNull SavedState` | `saveState()` Invokes state saving on all registered `SavedStateValue` instances and bundles their states into a single `SavedState`. |

## Public constructors

### SavedStateContainer

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

```
public SavedStateContainer()
```

## Public methods

### contains

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

```
public final boolean contains(@NonNull String key)
```

Checks if a `SavedStateValue` is registered under the given `key`.

| Parameters |
|---|---|
| `@NonNull String key` | unique identifier for the saved state value |

| Returns |
|---|---|
| `boolean` | `true` if a `SavedStateValue` is associated with `key`, `false` otherwise |

### getOrCreateContainer

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

```
public final @NonNull SavedStateContainer getOrCreateContainer(@NonNull String key)
```

Retrieves an existing nested `SavedStateContainer` associated with `key`, or creates and registers a new child `SavedStateContainer` if none exists.

| Parameters |
|---|---|
| `@NonNull String key` | unique identifier for the child container |

| Returns |
|---|---|
| `@NonNull SavedStateContainer` | existing or newly created child `SavedStateContainer` instance |

### getOrPutSavedStateValue

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

```
public final @NonNull R <T extends Object, R extends SavedStateValue<@NonNull T>> getOrPutSavedStateValue(
    @NonNull String key,
    @NonNull Function0<@NonNull R> defaultValue
)
```

Returns an existing `SavedStateValue` registered for `key`, or evaluates `defaultValue`, registers the result in this container, and returns it.

| Parameters |
|---|---|
| `@NonNull String key` | unique identifier for the saved state value |
| `@NonNull Function0<@NonNull R> defaultValue` | factory producing a new `SavedStateValue` if none is registered for `key` |

| Returns |
|---|---|
| `@NonNull R` | existing or newly registered `SavedStateValue` instance |

### getSavedStateValue

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

```
public final R <T extends Object, R extends SavedStateValue<@NonNull T>> getSavedStateValue(
    @NonNull String key
)
```

Retrieves the registered `SavedStateValue` associated with `key`.

| Parameters |
|---|---|
| `@NonNull String key` | unique identifier for the saved state value |

| Returns |
|---|---|
| `R` | registered `SavedStateValue` typed as `R`, or `null` if no value is found |

### keys

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

```
public final @NonNull Set<@NonNull String> keys()
```

Returns a `Set` containing all keys registered in this `SavedStateContainer`.

| Returns |
|---|---|
| `@NonNull Set<@NonNull String>` | set of registered keys |

### putSavedStateValue

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

```
public final void <T extends Object, R extends SavedStateValue<@NonNull T>> putSavedStateValue(
    @NonNull String key,
    @NonNull R savedStateValue
)
```

Registers a `savedStateValue` for state saving and restoration under the given `key`.

If a `SavedStateValue` was previously registered under `key`, it will be overwritten.

| Parameters |
|---|---|
| `@NonNull String key` | unique identifier for the saved state value |
| `@NonNull R savedStateValue` | component responsible for state saving and restoration |

### removeSavedStateValue

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

```
public final R <T extends Object, R extends SavedStateValue<@NonNull T>> removeSavedStateValue(
    @NonNull String key
)
```

Removes and returns the registered `SavedStateValue` associated with `key`.

| Parameters |
|---|---|
| `@NonNull String key` | unique identifier for the saved state value to remove |

| Returns |
|---|---|
| `R` | previously registered `SavedStateValue`, or `null` if none was registered |

### restoreState

```
public final void restoreState(SavedState savedState)
```

Restores state for all registered `SavedStateValue` instances using the provided `savedState`.

| Parameters |
|---|---|
| `SavedState savedState` | previously saved state container, or `null` if no state was saved |

### saveState

Added in [1.6.0-alpha05](https://developer.android.com/jetpack/androidx/releases/savedstate.md.txt#1.6.0-alpha05)

```
public final @NonNull SavedState saveState()
```

Invokes state saving on all registered `SavedStateValue` instances and bundles their states into a single `SavedState`.

| Returns |
|---|---|
| `@NonNull SavedState` | a `SavedState` containing the collected saved states of all registered providers |