TestLifecycleOwner

public final class TestLifecycleOwner implements 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 fields

final @NonNull Lifecycle.State

The current Lifecycle.State of this owner.

final int

Get the number of observers.

Public constructors

TestLifecycleOwner(
    Lifecycle.State initialState,
    CoroutineDispatcher coroutineDispatcher
)

Public methods

@NonNull LifecycleRegistry

Returns the Lifecycle of the provider.

final void

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

Public fields

currentState

public final @NonNull Lifecycle.State currentState

The current Lifecycle.State of this owner. This is safe to mutate on any thread.

observerCount

public final int observerCount

Get the number of observers.

Public constructors

TestLifecycleOwner

public final TestLifecycleOwner(
    Lifecycle.State initialState,
    CoroutineDispatcher coroutineDispatcher
)
Parameters
Lifecycle.State initialState

The initial Lifecycle.State.

Public methods

getLifecycle

public @NonNull LifecycleRegistry getLifecycle()

Returns the Lifecycle of the provider.

Returns
@NonNull LifecycleRegistry

The lifecycle of the provider.