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

Adds an OnDestinationChangedListener to this controller to receive a callback whenever the currentDestination or its arguments change.

final boolean

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

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
@MainThread
<T extends Object> clearBackStack(@NonNull T route)

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

final boolean

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

@NonNull NavDeepLinkBuilder

Create a deep link to a destination within this NavController.

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

Gets the topmost NavBackStackEntry for a route from KClass.

@NonNull NavBackStackEntry
getBackStackEntry(@IdRes int destinationId)

Gets the topmost NavBackStackEntry for a destination id.

final @NonNull NavBackStackEntry
<T extends Object> getBackStackEntry(@NonNull T route)

Gets the topmost NavBackStackEntry for a route from an Object.

final @NonNull NavBackStackEntry

Gets the topmost NavBackStackEntry for a route.

NavBackStackEntry

The topmost NavBackStackEntry.

final @NonNull Flow<@NonNull NavBackStackEntry>

A Flow that will emit the currently active NavBackStackEntry whenever it changes.

NavDestination

The current destination.

@NonNull NavGraph

The topmost navigation graph associated with this NavController.

@NonNull NavInflater

The inflater for this controller.

@NonNull NavigatorProvider

The NavController's NavigatorProvider.

NavBackStackEntry

The previous visible NavBackStackEntry.

@NonNull ViewModelStoreOwner
getViewModelStoreOwner(@IdRes int navGraphId)

Gets the ViewModelStoreOwner for a NavGraph.

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

A StateFlow that will emit the currently visible NavBackStackEntries whenever they change.

boolean

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

final boolean

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

void

Navigate to a destination via the given deep link Uri.

void

Navigate via the given NavDirections

void

Navigate to a destination via the given NavDeepLinkRequest.

void

Navigate to a destination from the current navigation graph.

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

Navigate to a destination via the given deep link Uri.

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

Navigate to a destination via the given NavDeepLinkRequest.

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
)

Navigate to a route from an Object in the current NavGraph.

final void
@MainThread
navigate(
    @NonNull String route,
    @ExtensionFunctionType @NonNull Function1<@NonNull NavOptionsBuilderUnit> builder
)

Navigate to a route in the current NavGraph.

void
@MainThread
navigate(
    @NonNull Uri deepLink,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)

Navigate to a destination via the given deep link Uri.

void
@MainThread
navigate(
    @NonNull NavDeepLinkRequest request,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)

Navigate to a destination via the given NavDeepLinkRequest.

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
)

Navigate to a route from an Object in the current NavGraph.

final void
@MainThread
navigate(
    @NonNull String route,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)

Navigate to a route in the current NavGraph.

void
@MainThread
navigate(
    @IdRes int resId,
    Bundle args,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)

Navigate to a destination from the current navigation graph.

boolean

Attempts to navigate up in the navigation hierarchy.

boolean

Attempts to pop the controller's back stack.

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)

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

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 T route,
    boolean inclusive,
    boolean saveState
)

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

final boolean
@MainThread
popBackStack(@NonNull String route, boolean inclusive, boolean saveState)

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

void

Removes an OnDestinationChangedListener from this controller.

void

Restores all navigation controller state from a bundle.

Bundle

Saves all navigation controller state to a Bundle.

void

The topmost navigation graph associated with this NavController.

void

Sets the navigation graph to the specified resource.

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

Sets the navigation graph to the specified graph.

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

Sets the host's LifecycleOwner.

final void

Sets the host's OnBackPressedDispatcher.

final void

Sets the host's ViewModelStore used by the NavController to store ViewModels at the navigation graph level.

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.