FragmentNavigator


@<Error class: unknown class>
public class FragmentNavigator extends 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

public final class FragmentNavigator.Extras implements Navigator.Extras

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

Builder for constructing new Extras instances.

Public constructors

FragmentNavigator(
    @NonNull Context context,
    @NonNull FragmentManager fragmentManager,
    int containerId
)

Public methods

@NonNull FragmentNavigator.Destination
@NonNull Fragment
instantiateFragment(
    @NonNull Context context,
    @NonNull FragmentManager fragmentManager,
    @NonNull String className,
    Bundle args
)

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

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

{@inheritDoc}

void
void

{@inheritDoc}

void
Bundle
void
popBackStack(@NonNull NavBackStackEntry popUpTo, boolean savedState)

{@inheritDoc}

Inherited methods

From androidx.navigation.Navigator
final @NonNull NavigatorState
final boolean
NavDestination
navigate(
    @NonNull FragmentNavigator.Destination destination,
    SavedState args,
    NavOptions navOptions,
    Navigator.Extras navigatorExtras
)
void
boolean
final void
setAttached(boolean isAttached)

Public constructors

FragmentNavigator

Added in 2.0.0
public FragmentNavigator(
    @NonNull Context context,
    @NonNull FragmentManager fragmentManager,
    int containerId
)

Public methods

createDestination

Added in 1.0.0
public @NonNull FragmentNavigator.Destination createDestination()

instantiateFragment

Added in 2.0.0
Deprecated in 2.1.0
public @NonNull Fragment instantiateFragment(
    @NonNull Context context,
    @NonNull FragmentManager fragmentManager,
    @NonNull String className,
    Bundle args
)

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
@NonNull Context context

Context providing the correct ClassLoader

@NonNull FragmentManager fragmentManager

FragmentManager the Fragment will be added to

@NonNull String className

The Fragment to instantiate

Bundle args

The Fragment's arguments, if any

Returns
@NonNull Fragment

A new fragment instance.

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

{@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

public void onAttach(@NonNull NavigatorState state)

onLaunchSingleTop

public void onLaunchSingleTop(@NonNull NavBackStackEntry backStackEntry)

{@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

public void onRestoreState(@NonNull Bundle savedState)

onSaveState

public Bundle onSaveState()

popBackStack

public void popBackStack(@NonNull NavBackStackEntry popUpTo, boolean savedState)

{@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.