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.

final void

Updates the currentState.

Public constructors

TestLifecycleOwner

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

The initial Lifecycle.State.

@NonNull CoroutineDispatcher coroutineDispatcher

A CoroutineDispatcher to use when dispatching work from this class.

Public methods

getCurrentState

Added in 2.3.0
public final @NonNull Lifecycle.State getCurrentState()

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).

getLifecycle

Added in 2.3.0
public @NonNull LifecycleRegistry getLifecycle()

Returns the Lifecycle of the provider.

Returns
@NonNull LifecycleRegistry

The lifecycle of the provider.

getObserverCount

Added in 2.3.0
public final int getObserverCount()

Get the number of observers.

handleLifecycleEvent

Added in 2.3.0
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 mutate on any thread, but will block that thread during execution.

setCurrentState

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

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).

setCurrentState

Added in 2.7.0
public final void setCurrentState(@NonNull Lifecycle.State state)

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.