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(
    @NonNull Lifecycle.State initialState,
    @NonNull 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

@NonNull
public final @NonNull Lifecycle.State currentState

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

observerCount

@NonNull
public final int observerCount

Get the number of observers.

Public constructors

TestLifecycleOwner

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

The initial Lifecycle.State.

Public methods

getLifecycle

@NonNull
public LifecycleRegistry getLifecycle()

Returns the Lifecycle of the provider.

Returns
LifecycleRegistry

The lifecycle of the provider.

handleLifecycleEvent

@NonNull
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.