# StateObject

Artifact: [androidx.compose.runtime:runtime](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/snapshots/Snapshot.kt+class:androidx.compose.runtime.snapshots.StateObject) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0) Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  interface StateObject
  ```
Known direct subclasses [NavBackStack](https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavBackStack), [SnapshotStateList](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateList), [SnapshotStateMap](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateMap), [SnapshotStateSet](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateSet)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavBackStack` | A mutable back stack of `https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavKey` elements that integrates with Compose state. |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateList` | An implementation of `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html` that can be observed and snapshot. |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateMap` | An implementation of `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-map/index.html` that can be observed and snapshot. |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateSet` | An implementation of `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html` that can be observed and snapshot. |

*** ** * ** ***

Interface implemented by all snapshot aware state objects. Used by this module to maintain the state records of a state object.

## Summary

| ### Public functions |
|---|---|---|
| `open https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord?` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#mergeRecords(androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord)( previous: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord, current: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord, applied: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord )` Produce a merged state based on the conflicting state changes. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#prependStateRecord(androidx.compose.runtime.snapshots.StateRecord)(value: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord)` Add a new state record to the beginning of a list. | Cmn |

| ### Public properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#firstStateRecord()` The first state record in a linked list of state records. | Cmn |

## Public functions

### mergeRecords

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
open fun mergeRecords(
    previous: StateRecord,
    current: StateRecord,
    applied: StateRecord
): StateRecord?
```

Produce a merged state based on the conflicting state changes.

This method must not modify any of the records received and should treat the state records as immutable, even the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#mergeRecords(androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord)` record.

| Parameters |
|---|---|
| `previous: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord` | the state record that was used to create the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#mergeRecords(androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord)` record and is a state that also (though indirectly) produced the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#mergeRecords(androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord)` record. |
| `current: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord` | the state record of the parent snapshot or global state. |
| `applied: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord` | the state record that is being applied of the parent snapshot or global state. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord?` | the modified state or `null` if the values cannot be merged. If the states cannot be merged the current apply will fail. Any of the parameters can be returned as a result. If it is not one of the parameter values then it *must* be a new value that is created by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord#create()` on one of the records passed and then can be modified to have the merged value before being returned. If a new record is returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` will update the internal snapshot id and call `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#prependStateRecord(androidx.compose.runtime.snapshots.StateRecord)` if the record is used. |

### prependStateRecord

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun prependStateRecord(value: StateRecord): Unit
```

Add a new state record to the beginning of a list. After this call `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#firstStateRecord()` should be `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#prependStateRecord(androidx.compose.runtime.snapshots.StateRecord)`.

## Public properties

### firstStateRecord

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
val firstStateRecord: StateRecord
```

The first state record in a linked list of state records.