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( |
Public methods |
|
|---|---|
final @NonNull Lifecycle.State |
The current |
@NonNull LifecycleRegistry |
Returns the Lifecycle of the provider. |
final int |
Get the number of observers. |
final void |
Update the |
final void |
setCurrentState(@NonNull Lifecycle.State currentState)The current |
final void |
setCurrentState(@NonNull Lifecycle.State state)Updates the |
Public constructors
TestLifecycleOwner
public TestLifecycleOwner(
@NonNull Lifecycle.State initialState,
@NonNull CoroutineDispatcher coroutineDispatcher
)
| Parameters | |
|---|---|
@NonNull Lifecycle.State initialState |
The initial |
@NonNull CoroutineDispatcher coroutineDispatcher |
A |
Public methods
getCurrentState
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
public @NonNull LifecycleRegistry getLifecycle()
Returns the Lifecycle of the provider.
| Returns | |
|---|---|
@NonNull LifecycleRegistry |
The lifecycle of the provider. |
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 mutate on any thread, but will block that thread during execution.
setCurrentState
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
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.