# BackgroundThreadStateHandler

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/util/BackgroundThreadStateHandler.java+class:androidx.media3.common.util.BackgroundThreadStateHandler)

*** ** * ** ***


```
@UnstableApi
class BackgroundThreadStateHandler<T : Any?>
```

<br />

*** ** * ** ***

Helper class to handle state updates on a background thread while maintaining a placeholder state on the foreground thread.

| Parameters |
|---|---|
| `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?>` | An immutable object representing the entire state. Must implement `https://developer.android.com/reference/java/lang/Object.html#equals(java.lang.Object)`. |

## Summary

| ### Nested types |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler.StateChangeListener<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?>` An interface to handle changes to the state on the foreground thread. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler#BackgroundThreadStateHandler(T,android.os.Looper,android.os.Looper,androidx.media3.common.util.Clock,androidx.media3.common.util.BackgroundThreadStateHandler.StateChangeListener<T>)( initialState: T!, backgroundLooper: https://developer.android.com/reference/android/os/Looper.html!, foregroundLooper: https://developer.android.com/reference/android/os/Looper.html!, clock: https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock!, onStateChanged: https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler.StateChangeListener<T!>! )` Creates the helper for background thread state updates. |

| ### Public functions |
|---|---|
| `T!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler#get()()` Returns the current state. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler#runInBackground(java.lang.Runnable)(runnable: https://developer.android.com/reference/java/lang/Runnable.html!)` Runs the provided `https://developer.android.com/reference/java/lang/Runnable.html` on the background thread. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler#setStateInBackground(T)(newState: T!)` Updates the background state directly, independent to any operation started from the foreground thread. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler#updateStateAsync(java.util.function.Function<T,T>,java.util.function.Function<T,T>)( placeholderState: https://developer.android.com/reference/java/util/function/Function.html<T!, T!>!, backgroundStateUpdate: https://developer.android.com/reference/java/util/function/Function.html<T!, T!>! )` Starts an asynchronous state update. |

## Public constructors

### BackgroundThreadStateHandler

```
BackgroundThreadStateHandler(
    initialState: T!,
    backgroundLooper: Looper!,
    foregroundLooper: Looper!,
    clock: Clock!,
    onStateChanged: BackgroundThreadStateHandler.StateChangeListener<T!>!
)
```

Creates the helper for background thread state updates.

This constructor may be called on any thread.

| Parameters |
|---|---|
| `initialState: T!` | The initial state value. |
| `backgroundLooper: https://developer.android.com/reference/android/os/Looper.html!` | The `https://developer.android.com/reference/android/os/Looper.html` to run background operations on. |
| `foregroundLooper: https://developer.android.com/reference/android/os/Looper.html!` | The `https://developer.android.com/reference/android/os/Looper.html` to run foreground operations on. |
| `clock: https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock` to control the handler messages. |
| `onStateChanged: https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler.StateChangeListener<T!>!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler.StateChangeListener` to listen to state changes. |

## Public functions

### get

```
fun get(): T!
```

Returns the current state.

Can be called on either the foreground or background thread, returning the respective current state of this thread.

### runInBackground

```
fun runInBackground(runnable: Runnable!): Unit
```

Runs the provided `https://developer.android.com/reference/java/lang/Runnable.html` on the background thread.

Can be called from any thread.

Note: This method is useful to update the state on the background using `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler#setStateInBackground(T)` for events arriving from external sources. Use `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BackgroundThreadStateHandler#updateStateAsync(java.util.function.Function<T,T>,java.util.function.Function<T,T>)` if the intention is update the state in response the a foreground thread method call.

| Parameters |
|---|---|
| `runnable: https://developer.android.com/reference/java/lang/Runnable.html!` | The `https://developer.android.com/reference/java/lang/Runnable.html` to be called on the background thread. |

### setStateInBackground

```
fun setStateInBackground(newState: T!): Unit
```

Updates the background state directly, independent to any operation started from the foreground thread.

Must only be called on the background thread.

| Parameters |
|---|---|
| `newState: T!` | The new state. |

### updateStateAsync

```
fun updateStateAsync(
    placeholderState: Function<T!, T!>!,
    backgroundStateUpdate: Function<T!, T!>!
): Unit
```

Starts an asynchronous state update.

Must only be called on the foreground thread.

| Parameters |
|---|---|
| `placeholderState: https://developer.android.com/reference/java/util/function/Function.html<T!, T!>!` | A function to create a placeholder state from the current state while the operation is pending. Will be called on the foreground thread. |
| `backgroundStateUpdate: https://developer.android.com/reference/java/util/function/Function.html<T!, T!>!` | A function to handle the background state update, taking in the current background state and returning the updated state. Will be called on the background thread. |