FragmentNavigator


@Navigator.Name(value = "fragment")
open class FragmentNavigator : Navigator

Known direct subclasses
DynamicFragmentNavigator

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


Navigator that navigates through fragment transactions. Every destination using this Navigator must set a valid Fragment class name with android:name or Destination.setClassName.

The current Fragment from FragmentNavigator's perspective can be retrieved by calling FragmentManager.getPrimaryNavigationFragment with the FragmentManager passed to this FragmentNavigator.

Note that the default implementation does Fragment transactions asynchronously, so the current Fragment will not be available immediately (i.e., in callbacks to NavController.OnDestinationChangedListener).

FragmentNavigator respects Log.isLoggable for debug logging, allowing you to use adb shell setprop log.tag.FragmentNavigator VERBOSE.

Summary

Nested types

NavDestination specific to FragmentNavigator

Extras that can be passed to FragmentNavigator to enable Fragment specific behavior

Builder for constructing new Extras instances.

Public constructors

FragmentNavigator(
    context: Context,
    fragmentManager: FragmentManager,
    containerId: Int
)

Public functions

open FragmentNavigator.Destination

Construct a new NavDestination associated with this Navigator.

open Fragment
instantiateFragment(
    context: Context,
    fragmentManager: FragmentManager,
    className: String,
    args: Bundle?
)

This function is deprecated. Set a custom {@link androidx.fragment.app.FragmentFactory} via {@link FragmentManager#setFragmentFactory(FragmentFactory)} to control instantiation of Fragments.

open Unit
navigate(
    entries: List<NavBackStackEntry>,
    navOptions: NavOptions?,
    navigatorExtras: Navigator.Extras?
)

{@inheritDoc}

open Unit

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

open Unit

{@inheritDoc}

open Unit
onRestoreState(savedState: Bundle)

Restore any state previously saved in onSaveState.

open Bundle?

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

open Unit
popBackStack(popUpTo: NavBackStackEntry, savedState: Boolean)

{@inheritDoc}

Inherited functions

From androidx.navigation.Navigator
open NavDestination?
navigate(
    destination: FragmentNavigator.Destination,
    args: Bundle?,
    navOptions: NavOptions?,
    navigatorExtras: Navigator.Extras?
)

Navigate to a destination.

open Boolean

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

Inherited properties

From androidx.navigation.Navigator
Boolean

Whether this Navigator is actively being used by a NavController.

NavigatorState

The state of the Navigator is the communication conduit between the Navigator and the NavController that has called onAttach.

Public constructors

FragmentNavigator

Added in 2.0.0
FragmentNavigator(
    context: Context,
    fragmentManager: FragmentManager,
    containerId: Int
)

Public functions

createDestination

Added in 1.0.0
open fun createDestination(): FragmentNavigator.Destination

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
FragmentNavigator.Destination

a new NavDestination

instantiateFragment

Added in 2.0.0
Deprecated in 2.1.0
open fun instantiateFragment(
    context: Context,
    fragmentManager: FragmentManager,
    className: String,
    args: Bundle?
): Fragment

Instantiates the Fragment via the FragmentManager's androidx.fragment.app.FragmentFactory.

Note that this method is not responsible for calling Fragment.setArguments on the returned Fragment instance.

Parameters
context: Context

Context providing the correct ClassLoader

fragmentManager: FragmentManager

FragmentManager the Fragment will be added to

className: String

The Fragment to instantiate

args: Bundle?

The Fragment's arguments, if any

Returns
Fragment

A new fragment instance.

open fun navigate(
    entries: List<NavBackStackEntry>,
    navOptions: NavOptions?,
    navigatorExtras: Navigator.Extras?
): Unit

{@inheritDoc}

This method should always call FragmentTransaction.setPrimaryNavigationFragment so that the Fragment associated with the new destination can be retrieved with FragmentManager.getPrimaryNavigationFragment.

Note that the default implementation commits the new Fragment asynchronously, so the new Fragment is not instantly available after this call completes.

This call will be ignored if the FragmentManager state has already been saved.

onAttach

open fun onAttach(state: NavigatorState): Unit

Indicator that this Navigator is actively being used by a NavController. This is called when the NavController's state is ready to be restored.

onLaunchSingleTop

open fun onLaunchSingleTop(backStackEntry: NavBackStackEntry): Unit

{@inheritDoc}

This method should always call FragmentTransaction.setPrimaryNavigationFragment so that the Fragment associated with the new destination can be retrieved with FragmentManager.getPrimaryNavigationFragment.

Note that the default implementation commits the new Fragment asynchronously, so the new Fragment is not instantly available after this call completes.

This call will be ignored if the FragmentManager state has already been saved.

onRestoreState

open fun onRestoreState(savedState: Bundle): Unit

Restore any state previously saved in onSaveState. This will be called before any calls to navigate or popBackStack.

Calls to createDestination should not be dependent on any state restored here as createDestination can be called before the state is restored.

Parameters
savedState: Bundle

The state previously saved

onSaveState

open fun onSaveState(): Bundle?

Called to ask for a Bundle representing the Navigator's state. This will be restored in onRestoreState.

popBackStack

open fun popBackStack(popUpTo: NavBackStackEntry, savedState: Boolean): Unit

{@inheritDoc}

This method must call FragmentTransaction.setPrimaryNavigationFragment if the pop succeeded so that the newly visible Fragment can be retrieved with FragmentManager.getPrimaryNavigationFragment.

Note that the default implementation pops the Fragment asynchronously, so the newly visible Fragment from the back stack is not instantly available after this call completes.