TestNavHostController


public final class TestNavHostController extends NavHostController


Subclass of NavHostController that offers additional APIs for testing Navigation.

Summary

Public constructors

Public methods

final @NonNull List<@NonNull NavBackStackEntry>

Gets an immutable copy of the elements currently on the back stack.

final void
setCurrentDestination(@IdRes int destId, @NonNull Bundle args)

Navigate directly to any destination on the current androidx.navigation.NavGraph via an explicit deep link.

final void

Navigate directly to any destination on the current androidx.navigation.NavGraph via an explicit deep link.

Inherited methods

From androidx.navigation.NavController
void
final boolean
final boolean
@MainThread
clearBackStack(@IdRes int destinationId)

Clears any saved state associated with destinationId that was previously saved via popBackStack when using a saveState value of true.

final boolean
final boolean
@MainThread
<T extends Object> clearBackStack(@NonNull T route)
final boolean
@NonNull NavDeepLinkBuilder

Create a deep link to a destination within this NavController.

final @NonNull NavBackStackEntry
<T extends Object> getBackStackEntry()
@NonNull NavBackStackEntry
getBackStackEntry(@IdRes int destinationId)

Gets the topmost NavBackStackEntry for a destination id.

final @NonNull NavBackStackEntry
<T extends Object> getBackStackEntry(@NonNull KClass<@NonNull T> route)
final @NonNull NavBackStackEntry
<T extends Object> getBackStackEntry(@NonNull T route)
final @NonNull NavBackStackEntry
NavBackStackEntry
final @NonNull Flow<@NonNull NavBackStackEntry>
NavDestination
@NonNull NavGraph
@NonNull NavInflater

The inflater for this controller.

@NonNull NavigatorProvider
NavBackStackEntry
@NonNull ViewModelStoreOwner
getViewModelStoreOwner(@IdRes int navGraphId)

Gets the ViewModelStoreOwner for a NavGraph.

final @NonNull StateFlow<@NonNull List<@NonNull NavBackStackEntry>>
boolean

Checks the given Intent for a Navigation deep link and navigates to the deep link if present.

final boolean
void
void

Navigate via the given NavDirections

void
void

Navigate to a destination from the current navigation graph.

void
@MainThread
navigate(@NonNull Uri deepLink, NavOptions navOptions)
void
@MainThread
navigate(@NonNull NavDirections directions, NavOptions navOptions)

Navigate via the given NavDirections

void
@MainThread
navigate(
    @NonNull NavDirections directions,
    @NonNull Navigator.Extras navigatorExtras
)

Navigate via the given NavDirections

void
void
@MainThread
navigate(@IdRes int resId, Bundle args)

Navigate to a destination from the current navigation graph.

final void
@MainThread
<T extends Object> navigate(
    @NonNull T route,
    @ExtensionFunctionType @NonNull Function1<@NonNull NavOptionsBuilderUnit> builder
)
final void
@MainThread
navigate(
    @NonNull String route,
    @ExtensionFunctionType @NonNull Function1<@NonNull NavOptionsBuilderUnit> builder
)
void
@MainThread
navigate(
    @NonNull Uri deepLink,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)
void
@MainThread
navigate(
    @NonNull NavDeepLinkRequest request,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)
void
@MainThread
navigate(@IdRes int resId, Bundle args, NavOptions navOptions)

Navigate to a destination from the current navigation graph.

final void
@MainThread
<T extends Object> navigate(
    @NonNull T route,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)
final void
@MainThread
navigate(
    @NonNull String route,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)
void
@MainThread
navigate(
    @IdRes int resId,
    Bundle args,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)

Navigate to a destination from the current navigation graph.

boolean
boolean
boolean
@MainThread
popBackStack(@IdRes int destinationId, boolean inclusive)

Attempts to pop the controller's back stack back to a specific destination.

final boolean
@MainThread
<T extends Object> popBackStack(boolean inclusive, boolean saveState)
boolean
@MainThread
popBackStack(
    @IdRes int destinationId,
    boolean inclusive,
    boolean saveState
)

Attempts to pop the controller's back stack back to a specific destination.

final boolean
@MainThread
<T extends Object> popBackStack(
    @NonNull KClass<@NonNull T> route,
    boolean inclusive,
    boolean saveState
)
final boolean
@MainThread
<T extends Object> popBackStack(
    @NonNull T route,
    boolean inclusive,
    boolean saveState
)
final boolean
@MainThread
popBackStack(@NonNull String route, boolean inclusive, boolean saveState)
void
void
Bundle
void
void

Sets the navigation graph to the specified resource.

void
@MainThread
@CallSuper
setGraph(@NonNull NavGraph graph, Bundle startDestinationArgs)
void
@MainThread
@CallSuper
setGraph(@NavigationRes int graphResId, Bundle startDestinationArgs)

Sets the navigation graph to the specified resource.

From androidx.navigation.NavHostController
final void
enableOnBackPressed(boolean enabled)

Set whether the NavController should handle the system Back button events via the registered OnBackPressedDispatcher.

final void
final void

Sets the host's OnBackPressedDispatcher.

final void

Public constructors

TestNavHostController

Added in 2.3.0
public TestNavHostController(@NonNull Context context)

Public methods

getBackStack

Added in 2.3.0
public final @NonNull List<@NonNull NavBackStackEntrygetBackStack()

Gets an immutable copy of the elements currently on the back stack.

setCurrentDestination

Added in 2.3.0
public final void setCurrentDestination(@IdRes int destId, @NonNull Bundle args)

Navigate directly to any destination on the current androidx.navigation.NavGraph via an explicit deep link. If an implicit deep link exists for this destination use #navigate(Uri) instead.

Parameters
@IdRes int destId

The destination id to navigate to.

@NonNull Bundle args

The arguments to pass to the destination.

Throws
java.lang.IllegalArgumentException

If the destination does not exist on the NavGraph.

setCurrentDestination

Added in 2.4.0
public final void setCurrentDestination(@NonNull String destRoute, @NonNull Bundle args)

Navigate directly to any destination on the current androidx.navigation.NavGraph via an explicit deep link. If an implicit deep link exists for this destination use #navigate(Uri) instead.

Parameters
@NonNull String destRoute

The destination route to navigate to.

@NonNull Bundle args

The arguments to pass to the destination.

Throws
java.lang.IllegalArgumentException

If the destination does not exist on the NavGraph.