# StateStore

Artifact: [androidx.appstate.statestore:statestore](https://developer.android.com/jetpack/androidx/releases/appstate-statestore) [View Source](https://cs.android.com/search?q=file:androidx/appstate/statestore/StateStore.kt+class:androidx.appstate.statestore.StateStore) Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

*** ** * ** ***


```
public final class StateStore
```

<br />

*** ** * ** ***

Class that contains a store that maintains compose state beyond individual Android components.

There is no limitation on where these instance should be instantiated as that is left up to the developer.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/appstate/statestore/StateStore#StateStore()()` |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey<@https://developer.android.com/reference/androidx/annotation/NonNull ?>>` | `https://developer.android.com/reference/androidx/appstate/statestore/StateStore#getKeys()()` The set of keys that are currently added to this StateStore. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/kotlin/androidx/compose/runtime/State.html<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/appstate/statestore/StateStore#getState(androidx.appstate.statestore.StateStoreKey,kotlin.Any)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey<@https://developer.android.com/reference/androidx/annotation/NonNull T> stateKey, @https://developer.android.com/reference/androidx/annotation/NonNull T defaultValue )` Returns the state associated with the given key. |
| `final void` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/appstate/statestore/StateStore#setState(androidx.appstate.statestore.StateStoreKey,kotlin.Any)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey<@https://developer.android.com/reference/androidx/annotation/NonNull T> stateKey, @https://developer.android.com/reference/androidx/annotation/NonNull T value )` Sets the state for the given key. |
| `final void` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/appstate/statestore/StateStore#updateState(androidx.appstate.statestore.StateStoreKey,kotlin.Any,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey<@https://developer.android.com/reference/androidx/annotation/NonNull T> stateKey, @https://developer.android.com/reference/androidx/annotation/NonNull T defaultValue, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull T, @https://developer.android.com/reference/androidx/annotation/NonNull T> update )` Retrieves the current state and uses that with the update lambda to set a new state for the specified key. |

| ### Extension functions |
|---|---|
| `final void` | `https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKt.https://developer.android.com/reference/androidx/appstate/statestore/StateStore#(androidx.appstate.statestore.StateStore).syncToDataStore(androidx.datastore.core.DataStore)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStore receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataStore<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/datastore/StateStorePreferences> dataStore )` Tracks changes to `https://developer.android.com/reference/androidx/appstate/statestore/StateStore` and syncs keys implementing `https://developer.android.com/reference/androidx/appstate/datastore/PersistToDataStore` to `https://developer.android.com/reference/androidx/datastore/core/DataStore`. |
| `final void` | `https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKt.https://developer.android.com/reference/androidx/appstate/statestore/StateStore#(androidx.appstate.statestore.StateStore).syncToDataStore(kotlin.String,kotlinx.coroutines.CoroutineScope)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStore receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html path, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html coroutineScope )` Tracks changes to `https://developer.android.com/reference/androidx/appstate/statestore/StateStore` and persists keys implementing `https://developer.android.com/reference/androidx/appstate/datastore/PersistToDataStore` to a `https://developer.android.com/reference/androidx/datastore/core/DataStore` backed by a file. |

## Public constructors

### StateStore

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
public StateStore()
```

## Public methods

### getKeys

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
public final @NonNull Set<@NonNull StateStoreKey<@NonNull ?>> getKeys()
```

The set of keys that are currently added to this StateStore.

This can be used in conjunction with the androidx.stateStore.transform APIs to provide a mechanism for reacting to state changes.

```
val scope = CoroutineScope(context + SupervisorJob())
val job = scope.launch {
    listen {
        val keys = stateStore.keys
        // Do some processing of the keys
    }
}
```

### getState

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
public final @NonNull State<@NonNull T> <T extends Object> getState(
    @NonNull StateStoreKey<@NonNull T> stateKey,
    @NonNull T defaultValue
)
```

Returns the state associated with the given key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey<@https://developer.android.com/reference/androidx/annotation/NonNull T> stateKey` | the key of the state to retrieve |
| `@https://developer.android.com/reference/androidx/annotation/NonNull T defaultValue` | the value to put if there is no value for the key |

### setState

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
public final void <T extends Object> setState(
    @NonNull StateStoreKey<@NonNull T> stateKey,
    @NonNull T value
)
```

Sets the state for the given key.

If this is the first time the state is being set a listener is added to allow the given key to be automatically cleared from the map based on the `https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey#autoClearKey()` and `https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey#shouldClearState()`. For the automatic clearing, `https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey#autoClearKey()` must be non-null, the value associated with this key must be non-null, and the `https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey#shouldClearState()` condition must return true. AS long as all 3 of those conditions are not met, the listener will not be removed.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey<@https://developer.android.com/reference/androidx/annotation/NonNull T> stateKey` | the key to set a value for |
| `@https://developer.android.com/reference/androidx/annotation/NonNull T value` | the value to set inside the StateStore |

### updateState

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
public final void <T extends Object> updateState(
    @NonNull StateStoreKey<@NonNull T> stateKey,
    @NonNull T defaultValue,
    @NonNull Function1<@NonNull T, @NonNull T> update
)
```

Retrieves the current state and uses that with the update lambda to set a new state for the specified key.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStoreKey<@https://developer.android.com/reference/androidx/annotation/NonNull T> stateKey` | the key to update |
| `@https://developer.android.com/reference/androidx/annotation/NonNull T defaultValue` | the value to use if no state has been set for the key |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull T, @https://developer.android.com/reference/androidx/annotation/NonNull T> update` | lambda used to update the state using the current state |

## Extension functions

### StateStoreKt.syncToDataStore

Artifact: [androidx.appstate.statestore:statestore-datastore](https://developer.android.com/jetpack/androidx/releases/appstate-statestore) [View Source](https://cs.android.com/search?q=file:androidx/appstate/datastore/StateStore.kt+function:syncToDataStore)

```
public final void StateStoreKt.syncToDataStore(
    @NonNull StateStore receiver,
    @NonNull DataStore<@NonNull StateStorePreferences> dataStore
)
```

Tracks changes to `https://developer.android.com/reference/androidx/appstate/statestore/StateStore` and syncs keys implementing `https://developer.android.com/reference/androidx/appstate/datastore/PersistToDataStore` to `https://developer.android.com/reference/androidx/datastore/core/DataStore`.

Keys must implement `https://developer.android.com/reference/androidx/appstate/datastore/PersistToDataStore` to be saved.

```kotlin
import androidx.appstate.datastore.syncToDataStore
import androidx.appstate.statestore.StateStore
import androidx.datastore.core.DataStore

val stateStore = StateStore()

// Register the key by getting the state
val state = stateStore.getState(SampleKey)

stateStore.syncToDataStore(dataStore)
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataStore<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/datastore/StateStorePreferences> dataStore` | the `https://developer.android.com/reference/androidx/datastore/core/DataStore` used to save and restore state |

### StateStoreKt.syncToDataStore

Artifact: [androidx.appstate.statestore:statestore-datastore](https://developer.android.com/jetpack/androidx/releases/appstate-statestore) [View Source](https://cs.android.com/search?q=file:androidx/appstate/datastore/StateStore.kt+function:syncToDataStore)

```
public final void StateStoreKt.syncToDataStore(
    @NonNull StateStore receiver,
    @NonNull String path,
    @NonNull CoroutineScope coroutineScope
)
```

Tracks changes to `https://developer.android.com/reference/androidx/appstate/statestore/StateStore` and persists keys implementing `https://developer.android.com/reference/androidx/appstate/datastore/PersistToDataStore` to a `https://developer.android.com/reference/androidx/datastore/core/DataStore` backed by a file.

Keys must implement `https://developer.android.com/reference/androidx/appstate/datastore/PersistToDataStore` to be saved.

```kotlin
import androidx.appstate.datastore.syncToDataStore
import androidx.appstate.statestore.StateStore
import androidx.datastore.core.DataStore

val stateStore = StateStore()

// Register the key by getting the state
val state = stateStore.getState(SampleKey)

// The path must include the directory, for example, on Android:
// val path = context.filesDir.resolve("stateStore.preferences_pb").absolutePath
val path = "/path/to/directory/stateStore.preferences_pb"

stateStore.syncToDataStore(path, CoroutineScope(Dispatchers.IO))
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html path` | the file path to save and restore state from |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html coroutineScope` | the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` used for the `https://developer.android.com/reference/androidx/datastore/core/DataStore` |