{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

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 constructors

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

Public methods

final @NonNull Lifecycle.State

The current Lifecycle.State of this owner.

@NonNull LifecycleRegistry

Returns the Lifecycle of the provider.

final int

Get the number of observers.

final void

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

final void

The current Lifecycle.State of this owner.

Public constructors

TestLifecycleOwner

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

The initial Lifecycle.State.

Public methods

getCurrentState

public final @NonNull Lifecycle.State getCurrentState()

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

getLifecycle

public @NonNull LifecycleRegistry getLifecycle()

Returns the Lifecycle of the provider.

Returns
@NonNull LifecycleRegistry

The lifecycle of the provider.

getObserverCount

public final int getObserverCount()

Get the number of observers.

handleLifecycleEvent

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

Update the currentState by moving it to the state directly after the given event. This is safe to call on any thread.

setCurrentState

public final void setCurrentState(@NonNull Lifecycle.State currentState)

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