Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

FragmentNavigator

public class FragmentNavigator
extends Navigator<FragmentNavigator.Destination>

java.lang.Object
   ↳ androidx.navigation.Navigator<androidx.navigation.fragment.FragmentNavigator.Destination>
     ↳ androidx.navigation.fragment.FragmentNavigator


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

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).

Summary

Nested classes

class FragmentNavigator.Destination

NavDestination specific to FragmentNavigator  

class FragmentNavigator.Extras

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

Public constructors

FragmentNavigator(Context context, FragmentManager manager, int containerId)

Public methods

FragmentNavigator.Destination createDestination()

Construct a new NavDestination associated with this Navigator.

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

This method is deprecated. Set a custom FragmentFactory via FragmentManager.setFragmentFactory(FragmentFactory) to control instantiation of Fragments.

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

Navigate to a destination.

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

void onRestoreState(Bundle savedState)

Restore any state previously saved in onSaveState().

Bundle onSaveState()

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

boolean popBackStack()

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

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

Inherited methods

Public constructors

FragmentNavigator

public FragmentNavigator (Context context, 
                FragmentManager manager, 
                int containerId)

Parameters
context Context

manager FragmentManager

containerId int

Public methods

createDestination

public FragmentNavigator.Destination createDestination ()

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

public Fragment instantiateFragment (Context context, 
                FragmentManager fragmentManager, 
                String className, 
                Bundle args)

This method is deprecated.
Set a custom FragmentFactory via FragmentManager.setFragmentFactory(FragmentFactory) to control instantiation of Fragments.

Instantiates the Fragment via the FragmentManager's FragmentFactory. Note that this method is not responsible for calling Fragment.setArguments(Bundle) 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.

public NavDestination navigate (FragmentNavigator.Destination destination, 
                Bundle args, 
                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.

This method should always call FragmentTransaction.setPrimaryNavigationFragment(Fragment) 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.

Parameters
destination FragmentNavigator.Destination: destination node to navigate to

args Bundle: arguments to use for navigation

navOptions NavOptions: additional options for navigation

navigatorExtras Navigator.Extras: extras unique to your Navigator.

Returns
NavDestination The NavDestination that should be added to the back stack or null if no change was made to the back stack (i.e., in cases of single top operations where the destination is already on top of the back stack).

onRestoreState

public void onRestoreState (Bundle savedState)

Restore any state previously saved in onSaveState(). This will be called before any calls to navigate(NavDestination, Bundle, NavOptions, Navigator.Extras) 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

public Bundle onSaveState ()

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

Returns
Bundle

popBackStack

public boolean popBackStack ()

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

Implementations should return true if navigation was successful. Implementations should return false if navigation could not be performed, for example if the navigator's back stack was empty.

This method must call FragmentTransaction.setPrimaryNavigationFragment(Fragment) 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.

Returns
boolean true if pop was successful