TestLifecycleOwner

class TestLifecycleOwner : LifecycleOwner


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
)

Public functions

Unit

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

suspend Unit

Updates the currentState.

Public properties

Lifecycle.State

The current Lifecycle.State of this owner.

open LifecycleRegistry

Returns the Lifecycle of the provider.

Int

Get the number of observers.

Public constructors

TestLifecycleOwner

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

The initial Lifecycle.State.

Public functions

handleLifecycleEvent

Added in 2.3.0
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

Added in 2.7.0
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

Added in 2.3.0
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

Added in 2.3.0
open val lifecycleLifecycleRegistry

Returns the Lifecycle of the provider.

Returns
LifecycleRegistry

The lifecycle of the provider.

observerCount

Added in 2.3.0
val observerCountInt

Get the number of observers.