# 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)

*** ** * ** ***


```
public final class TestLifecycleOwner implements LifecycleOwner
```

<br />

*** ** * ** ***

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

Under the hood, this uses a `https://developer.android.com/reference/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/androidx/lifecycle/testing/TestLifecycleOwner#(androidx.lifecycle.Lifecycle.State,kotlinx.coroutines.CoroutineDispatcher)` to ensure that all mutations to the `https://developer.android.com/reference/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/androidx/lifecycle/testing/TestLifecycleOwner#TestLifecycleOwner(androidx.lifecycle.Lifecycle.State,kotlinx.coroutines.CoroutineDispatcher)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State initialState, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html coroutineDispatcher )` |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` | `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#getCurrentState()()` The current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` of this owner. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry` | `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#getLifecycle()()` The `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` of this owner. |
| `final int` | `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#getObserverCount()()` Get the number of observers. |
| `final void` | `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event event)` Update the `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#currentState()` by moving it to the state directly after the given `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event)`. |
| `final void` | `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#setCurrentState(androidx.lifecycle.Lifecycle.State)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State state)` Updates the `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#currentState()`. |
| `final void` | `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#setCurrentState(androidx.lifecycle.Lifecycle.State)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State value)` The current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` of this owner. |

## Public constructors

### TestLifecycleOwner

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

```
public TestLifecycleOwner(
    @NonNull Lifecycle.State initialState,
    @NonNull CoroutineDispatcher coroutineDispatcher
)
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State initialState` | The initial `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html coroutineDispatcher` | 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 methods

### getCurrentState

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

```
public final @NonNull Lifecycle.State getCurrentState()
```

The current `https://developer.android.com/reference/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/androidx/lifecycle/testing/TestLifecycleOwner#setCurrentState(androidx.lifecycle.Lifecycle.State)` instead).

### getLifecycle

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

```
public @NonNull LifecycleRegistry getLifecycle()
```

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

### getObserverCount

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

```
public final int getObserverCount()
```

Get the number of observers.

### handleLifecycleEvent

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

```
public final void handleLifecycleEvent(@NonNull Lifecycle.Event event)
```

Update the `https://developer.android.com/reference/androidx/lifecycle/testing/TestLifecycleOwner#currentState()` by moving it to the state directly after the given `https://developer.android.com/reference/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

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

```
public final void setCurrentState(@NonNull Lifecycle.State state)
```

Updates the `https://developer.android.com/reference/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/androidx/lifecycle/testing/TestLifecycleOwner#currentState()` property syntax instead.

### setCurrentState

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

```
public final void setCurrentState(@NonNull Lifecycle.State value)
```

The current `https://developer.android.com/reference/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/androidx/lifecycle/testing/TestLifecycleOwner#setCurrentState(androidx.lifecycle.Lifecycle.State)` instead).