Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

FragmentNavigator

open class FragmentNavigator : Navigator<FragmentNavigator.Destination!>
kotlin.Any
   ↳ 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 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
open

NavDestination specific to FragmentNavigator

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

Public constructors
<init>(@NonNull context: Context, @NonNull manager: FragmentManager, containerId: Int)

Public methods
open FragmentNavigator.Destination

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

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

open NavDestination?
navigate(@NonNull destination: FragmentNavigator.Destination, @Nullable args: Bundle?, @Nullable navOptions: NavOptions?, @Nullable navigatorExtras: Navigator.Extras?)

Navigate to a destination.

open Unit
onRestoreState(@Nullable savedState: Bundle!)

open Bundle?

open Boolean

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

Protected methods
open Unit

open Unit

Public constructors

<init>

FragmentNavigator(@NonNull context: Context, @NonNull manager: FragmentManager, containerId: Int)

Public methods

createDestination

@NonNull open fun createDestination(): FragmentNavigator.Destination

instantiateFragment

@NonNull open fun instantiateFragment(@NonNull context: Context, @NonNull fragmentManager: FragmentManager, @NonNull className: String, @Nullable 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(Bundle) on the returned Fragment instance.

Parameters
context Context: Context providing the correct ClassLoader
fragmentManager Context: FragmentManager the Fragment will be added to
className Context: The Fragment to instantiate
args Context: The Fragment's arguments, if any
Return
Fragment: A new fragment instance.

navigate

@Nullable open fun navigate(@NonNull destination: FragmentNavigator.Destination, @Nullable args: Bundle?, @Nullable navOptions: NavOptions?, @Nullable navigatorExtras: Navigator.Extras?): NavDestination?

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 FragmentNavigator.Destination: arguments to use for navigation
navOptions FragmentNavigator.Destination: additional options for navigation
navigatorExtras FragmentNavigator.Destination: extras unique to your Navigator.
Return
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

open fun onRestoreState(@Nullable savedState: Bundle!): Unit

onSaveState

@Nullable open fun onSaveState(): Bundle?

popBackStack

open fun popBackStack(): Boolean

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.

Return
Boolean: true if pop was successful

Protected methods

onBackPressAdded

protected open fun onBackPressAdded(): Unit

onBackPressRemoved

protected open fun onBackPressRemoved(): Unit