Navigator

public abstract class Navigator<D extends NavDestination>

Known direct subclasses
ActivityNavigator

ActivityNavigator implements cross-activity navigation.

DialogFragmentNavigator

Navigator that uses DialogFragment.show.

DynamicIncludeGraphNavigator

Navigator for include-dynamic.

FragmentNavigator

Navigator that navigates through FragmentTransaction.

NavGraphNavigator

A Navigator built specifically for NavGraph elements.

Known indirect subclasses
DynamicActivityNavigator

Dynamic feature navigator for Activity destinations.

DynamicFragmentNavigator

The Navigator that enables navigating to destinations within dynamic feature modules.

DynamicGraphNavigator

Navigator for graphs in dynamic feature modules.


Navigator defines a mechanism for navigating within an app.

Each Navigator sets the policy for a specific type of navigation, e.g. ActivityNavigator knows how to launch into destinations backed by activities using Context.startActivity.

Navigators should be able to manage their own back stack when navigating between two destinations that belong to that navigator. The NavController manages a back stack of navigators representing the current navigation stack across all navigators.

Each Navigator should add the Navigator.Name annotation to their class. Any custom attributes used by the associated destination subclass should have a name corresponding with the name of the Navigator, e.g., ActivityNavigator uses <declare-styleable name="ActivityNavigator">

Parameters
<D extends NavDestination>

the subclass of NavDestination used with this Navigator which can be used to hold any special data that will be needed to navigate to that destination. Examples include information about an intent to navigate to other activities, or a fragment class name to instantiate and swap to a new fragment.

Summary

Nested types

Navigator.Extras

Interface indicating that this class should be passed to its respective Navigator to enable Navigator specific behavior.

Navigator.Name

This annotation should be added to each Navigator subclass to denote the default name used to register the Navigator with a NavigatorProvider.

Public fields

final boolean

Whether this Navigator is actively being used by a NavController.

Public constructors

<D extends NavDestination> Navigator()

Public methods

abstract @NonNull D

Construct a new NavDestination associated with this Navigator.

void
navigate(
    List<NavBackStackEntry> entries,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)

Navigate to a destination.

NavDestination
navigate(
    D destination,
    Bundle args,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)

Navigate to a destination.

@CallSuper void

Indicator that this Navigator is actively being used by a NavController.

void

Informational callback indicating that the given backStackEntry has been affected by a NavOptions.shouldLaunchSingleTop operation.

void
onRestoreState(Bundle savedState)

Restore any state previously saved in onSaveState.

Bundle

Called to ask for a Bundle representing the Navigator's state.

void
popBackStack(NavBackStackEntry popUpTo, boolean savedState)

Attempt to pop this navigator's back stack, performing the appropriate navigation.

boolean

Attempt to pop this navigator's back stack, performing the appropriate navigation.

Public fields

isAttached

public final boolean isAttached

Whether this Navigator is actively being used by a NavController.

This is set to true when onAttach is called.

Public constructors

public final <D extends NavDestination> Navigator()
Parameters
<D extends NavDestination>

the subclass of NavDestination used with this Navigator which can be used to hold any special data that will be needed to navigate to that destination. Examples include information about an intent to navigate to other activities, or a fragment class name to instantiate and swap to a new fragment.

Public methods

createDestination

public abstract @NonNullcreateDestination()

Construct a new NavDestination associated with this Navigator.

Any initialization of the destination should be done in the destination's constructor as it is not guaranteed that every destination will be created through this method.

Returns
@NonNull D

a new NavDestination

public void navigate(
    List<NavBackStackEntry> entries,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)

Navigate to a destination.

Requests navigation to a given destination associated with this navigator in the navigation graph. This method generally should not be called directly; NavController will delegate to it when appropriate.

Parameters
List<NavBackStackEntry> entries

destination(s)