# TestLifecycleOwner

Artifact: [androidx.lifecycle:lifecycle-runtime-testing](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/testing/TestLifecycleOwner.kt+class:androidx.lifecycle.testing.TestLifecycleOwner) Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.3.0)

*** ** * ** ***

Common/All
- Cmn

  ```
  class TestLifecycleOwner : LifecycleOwner
  ```

*** ** * ** ***

Create a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` that allows changing the state via the `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event)` method or `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#currentState()` property.

Under the hood, this uses a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleRegistry`. However, it uses `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-coroutine-dispatcher/immediate.html` as the default `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#(androidx.lifecycle.Lifecycle.State,kotlinx.coroutines.CoroutineDispatcher)` to ensure that all mutations to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#currentState()` are run on that dispatcher, no matter what thread you mutate the state from.

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#TestLifecycleOwner(androidx.lifecycle.Lifecycle.State,kotlinx.coroutines.CoroutineDispatcher)( initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, coroutineDispatcher: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html )` | Cmn |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event)(event: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.Event)` Update the `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#currentState()` by moving it to the state directly after the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event)`. | Cmn |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#setCurrentState(androidx.lifecycle.Lifecycle.State)(state: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State)` Updates the `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#currentState()`. | Cmn |

| ### Public properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#currentState()` The current `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of this owner. | Cmn |
| `open https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleRegistry` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#lifecycle()` The `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` of this owner. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#observerCount()` Get the number of observers. | Cmn |

## Public constructors

### TestLifecycleOwner

Cmn Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.3.0)

```
TestLifecycleOwner(
    initialState: Lifecycle.State = Lifecycle.State.STARTED,
    coroutineDispatcher: CoroutineDispatcher = Dispatchers.Main.immediate
)
```

| Parameters |
|---|---|
| `initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = Lifecycle.State.STARTED` | The initial `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State`. |
| `coroutineDispatcher: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html = Dispatchers.Main.immediate` | A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html` to use when dispatching work from this class. |

## Public functions

### handleLifecycleEvent

Cmn Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.3.0)

```
fun handleLifecycleEvent(event: Lifecycle.Event): Unit
```

Update the `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#currentState()` by moving it to the state directly after the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event)`. This is safe to mutate on any thread, but will block that thread during execution.

### setCurrentState

Cmn Added in [2.7.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.7.0)

```
suspend fun setCurrentState(state: Lifecycle.State): Unit
```

Updates the `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#currentState()`. This suspending function is safe to call on any thread and will not block that thread. If the state should be updated from outside of a suspending function, use `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#currentState()` property syntax instead.

## Public properties

### currentState

Cmn Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.3.0)

```
var currentState: Lifecycle.State
```

The current `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State` of this owner. This is safe to call on any thread but is thread-blocking and should not be called from within a coroutine (use `https://developer.android.com/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner#setCurrentState(androidx.lifecycle.Lifecycle.State)` instead).

### lifecycle

Cmn Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.3.0)

```
open val lifecycle: LifecycleRegistry
```

The `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` of this owner.

### observerCount

Cmn Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.3.0)

```
val observerCount: Int
```

Get the number of observers.