TestNavigatorState


public final class TestNavigatorState extends NavigatorState


An implementation of NavigatorState that allows testing a androidx.navigation.Navigator in isolation (i.e., without requiring a androidx.navigation.NavController).

An optional context can be provided to allow for the usages of androidx.lifecycle.AndroidViewModel within the created NavBackStackEntry instances.

The Lifecycle of all NavBackStackEntry instances added to this TestNavigatorState will be updated as they are added and removed from the state. This work is kicked off on the coroutineDispatcher.

Summary

Public constructors

TestNavigatorState(
    Context context,
    @NonNull CoroutineDispatcher coroutineDispatcher
)

Public methods

@NonNull NavBackStackEntry
createBackStackEntry(
    @NonNull NavDestination destination,
    Bundle arguments
)
void
void
pop(@NonNull NavBackStackEntry popUpTo, boolean saveState)
void
popWithTransition(@NonNull NavBackStackEntry popUpTo, boolean saveState)
void
void
push(@NonNull NavBackStackEntry backStackEntry)
final @NonNull NavBackStackEntry

Restore a previously saved NavBackStackEntry.

Public constructors

TestNavigatorState

Added in 2.4.0
public TestNavigatorState(
    Context context,
    @NonNull CoroutineDispatcher coroutineDispatcher
)

Public methods

createBackStackEntry

Added in 2.4.0
public @NonNull NavBackStackEntry createBackStackEntry(
    @NonNull NavDestination destination,
    Bundle arguments
)

markTransitionComplete

public void markTransitionComplete(@NonNull NavBackStackEntry entry)

pop

public void pop(@NonNull NavBackStackEntry popUpTo, boolean saveState)

popWithTransition

public void popWithTransition(@NonNull NavBackStackEntry popUpTo, boolean saveState)

prepareForTransition

public void prepareForTransition(@NonNull NavBackStackEntry entry)

push

public void push(@NonNull NavBackStackEntry backStackEntry)

restoreBackStackEntry

Added in 2.4.0
public final @NonNull NavBackStackEntry restoreBackStackEntry(@NonNull NavBackStackEntry previouslySavedEntry)

Restore a previously saved NavBackStackEntry. You must have previously called pop with previouslySavedEntry and true.