NavigatorState

public abstract class NavigatorState

Known direct subclasses
TestNavigatorState

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


The NavigatorState encapsulates the state shared between the Navigator and the NavController.

Summary

Public constructors

Public methods

abstract @NonNull NavBackStackEntry
createBackStackEntry(
    @NonNull NavDestination destination,
    Bundle arguments
)

Create a new NavBackStackEntry from a given destination and arguments.

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

While the NavController is responsible for the combined back stack across all Navigators, this back stack is specifically the set of destinations associated with this Navigator.

final @NonNull StateFlow<@NonNull Set<@NonNull NavBackStackEntry>>

This is the set of currently running transitions.

void

This removes the given NavBackStackEntry from the set of the transitions in progress.

void

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

void

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

void
pop(@NonNull NavBackStackEntry popUpTo, boolean saveState)

Pop all destinations up to and including popUpTo.

void
popWithTransition(@NonNull NavBackStackEntry popUpTo, boolean saveState)

Pops all destinations up to and including popUpTo.

void

This prepares the given NavBackStackEntry for transition.

void
push(@NonNull NavBackStackEntry backStackEntry)

Adds the given backStackEntry to the backStack.

void

Adds the given backStackEntry to the backStack.

Public constructors

Added in 2.4.0
public NavigatorState()

Public methods

createBackStackEntry

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

Create a new NavBackStackEntry from a given destination and arguments.

getBackStack

Added in 2.4.0
public final @NonNull StateFlow<@NonNull List<@NonNull NavBackStackEntry>> getBackStack()

While the NavController is responsible for the combined back stack across all Navigators, this back stack is specifically the set of destinations associated with this Navigator.

Changing the back stack must be done via push and pop.

getTransitionsInProgress

Added in 2.4.0
public final @NonNull StateFlow<@NonNull Set<@NonNull NavBackStackEntry>> getTransitionsInProgress()

This is the set of currently running transitions. Use this set to retrieve the entry and call markTransitionComplete once the transition is complete.

markTransitionComplete

Added in 2.4.0
public void markTransitionComplete(@NonNull NavBackStackEntry entry)

This removes the given NavBackStackEntry from the set of the transitions in progress. This should be called in conjunction with pushWithTransition and popWithTransition as those call are responsible for adding entries to transitionsInProgress.

This should also always be called in conjunction with prepareForTransition to ensure all NavBackStackEntries settle into the proper state.

Failing to call this method could result in entries being prevented from reaching their final Lifecycle.State}.

onLaunchSingleTop

Added in 2.4.0
@CallSuper
public void onLaunchSingleTop(@NonNull NavBackStackEntry backStackEntry)

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

Replaces the topmost entry with same id with the new backStackEntry

Parameters
@NonNull NavBackStackEntry backStackEntry

the NavBackStackEntry to replace the old Entry within the backStack

onLaunchSingleTopWithTransition

Added in 2.6.0
@CallSuper
public void onLaunchSingleTopWithTransition(
    @NonNull NavBackStackEntry backStackEntry
)

Informational callback indicating that the given backStackEntry has been affected by a NavOptions.shouldLaunchSingleTop operation. This also adds the given and previous entry to the set of in progress transitions. Added entries have their Lifecycle capped at Lifecycle.State.STARTED until an entry is passed into the markTransitionComplete callback, when they are allowed to go to Lifecycle.State.RESUMED while previous entries have their Lifecycle held at Lifecycle.State.CREATED until an entry is passed into the markTransitionComplete callback, when they are allowed to go to Lifecycle.State.DESTROYED and have their state cleared.

Replaces the topmost entry with same id with the new backStackEntry

Parameters
@NonNull NavBackStackEntry backStackEntry

the NavBackStackEntry to replace the old Entry within the backStack

pop

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

Pop all destinations up to and including popUpTo. This will remove those destinations from the backStack, saving their state if saveState is true.

popWithTransition

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

Pops all destinations up to and including popUpTo. This also adds the given and incoming entry to the set of in progress transitions. Added entries have their Lifecycle held at Lifecycle.State.CREATED until an entry is passed into the markTransitionComplete callback, when they are allowed to go to Lifecycle.State.DESTROYED and have their state cleared.

This will remove those destinations from the backStack, saving their state if saveState is true.

prepareForTransition

Added in 2.6.0
@CallSuper
public void prepareForTransition(@NonNull NavBackStackEntry entry)

This prepares the given NavBackStackEntry for transition. This should be called in conjunction with markTransitionComplete as that is responsible for settling the NavBackStackEntry into its final state.

push

Added in 2.4.0
public void push(@NonNull NavBackStackEntry backStackEntry)

Adds the given backStackEntry to the backStack.

pushWithTransition

Added in 2.4.0
public void pushWithTransition(@NonNull NavBackStackEntry backStackEntry)

Adds the given backStackEntry to the backStack. This also adds the given and previous entry to the set of in progress transitions. Added entries have their Lifecycle capped at Lifecycle.State.STARTED until an entry is passed into the markTransitionComplete callback, when they are allowed to go to Lifecycle.State.RESUMED.