TestLifecycleOwner



Create a LifecycleOwner that allows changing the state via the handleLifecycleEvent method or currentState property.

Under the hood, this uses a LifecycleRegistry. However, it uses Dispatchers.Main.immediate as the default coroutineDispatcher to ensure that all mutations to the current state are run on that dispatcher, no matter what thread you mutate the state from.

Summary

Public constructors

TestLifecycleOwner(
    initialState: Lifecycle.State,
    coroutineDispatcher: CoroutineDispatcher
)
Cmn

Public functions

Unit

Update the currentState by moving it to the state directly after the given event.

Cmn
suspend Unit

Updates the currentState.

Cmn

Public properties

Lifecycle.State

The current Lifecycle.State of this owner.

Cmn
open LifecycleRegistry

Returns the Lifecycle of the provider.

Cmn
Int

Get the number of observers.

Cmn

Public constructors

TestLifecycleOwner

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

The initial Lifecycle.State.

coroutineDispatcher: CoroutineDispatcher = Dispatchers.Main.immediate

A CoroutineDispatcher to use when dispatching work from this class.

Public functions

handleLifecycleEvent

fun handleLifecycleEvent(event: Lifecycle.Event): Unit

Update the currentState by moving it to the state directly after the given event. This is safe to mutate on any thread, but will block that thread during execution.

setCurrentState

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

Updates the 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 currentState property syntax instead.

Public properties

currentState

var currentStateLifecycle.State

The current 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 setCurrentState instead).

lifecycle

open val lifecycleLifecycleRegistry

Returns the Lifecycle of the provider.

Returns
LifecycleRegistry

The lifecycle of the provider.

observerCount

val observerCountInt

Get the number of observers.