# FragmentNavigator

Artifact: [androidx.navigation:navigation-fragment](https://developer.android.com/jetpack/androidx/releases/navigation) [View Source](https://cs.android.com/search?q=file:androidx/navigation/fragment/FragmentNavigator.kt+class:androidx.navigation.fragment.FragmentNavigator) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/navigation#1.0.0)

*** ** * ** ***


```
@Navigator.Name(value = "fragment")
public class FragmentNavigator extends Navigator<@NonNull FragmentNavigator.Destination>
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.navigation.Navigator](https://developer.android.com/reference/androidx/navigation/Navigator)\<@[NonNull](https://developer.android.com/reference/androidx/annotation/NonNull) [FragmentNavigator.Destination](https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator.Destination)\> ||
|   | ↳ | [androidx.navigation.fragment.FragmentNavigator](https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator) |

Known direct subclasses [DynamicFragmentNavigator](https://developer.android.com/reference/androidx/navigation/dynamicfeatures/fragment/DynamicFragmentNavigator)

|---|---|
| `https://developer.android.com/reference/androidx/navigation/dynamicfeatures/fragment/DynamicFragmentNavigator` | The `https://developer.android.com/reference/androidx/navigation/Navigator` that enables navigating to destinations within dynamic feature modules. |

*** ** * ** ***

Navigator that navigates through `https://developer.android.com/reference/androidx/fragment/app/FragmentTransaction`. Every destination using this Navigator must set a valid Fragment class name with `android:name` or `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator.Destination#setClassName(kotlin.String)`.

The current Fragment from FragmentNavigator's perspective can be retrieved by calling `https://developer.android.com/reference/androidx/fragment/app/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 `https://developer.android.com/reference/androidx/navigation/NavController.OnDestinationChangedListener`).

FragmentNavigator respects `https://developer.android.com/reference/android/util/Log.html#isLoggable(java.lang.String, int)` for debug logging, allowing you to use `adb shell setprop log.tag.FragmentNavigator VERBOSE`.

## Summary

| ### Nested types |
|---|
| `@https://developer.android.com/reference/androidx/navigation/NavDestination.ClassType(value = https://developer.android.com/reference/androidx/fragment/app/Fragment) public class https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator.Destination extends https://developer.android.com/reference/androidx/navigation/NavDestination` NavDestination specific to `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator` |
| `public final class https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator.Extras implements https://developer.android.com/reference/androidx/navigation/Navigator.Extras` Extras that can be passed to FragmentNavigator to enable Fragment specific behavior |
| `public final class https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator.Extras.Builder` Builder for constructing new `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator.Extras` instances. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#FragmentNavigator(android.content.Context,androidx.fragment.app.FragmentManager,kotlin.Int)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/FragmentManager fragmentManager, int containerId )` |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator.Destination` | `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#createDestination()()` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment` | `[instantiateFragment](https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#instantiateFragment(android.content.Context,androidx.fragment.app.FragmentManager,kotlin.String,android.os.Bundle))( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/FragmentManager fragmentManager, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html className, https://developer.android.com/reference/android/os/Bundle.html args )` **This method is deprecated.** Set a custom {@link androidx.fragment.app.FragmentFactory} via {@link FragmentManager#setFragmentFactory(FragmentFactory)} to control instantiation of Fragments. |
| `void` | `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#navigate(kotlin.collections.List,androidx.navigation.NavOptions,androidx.navigation.Navigator.Extras)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/navigation/NavBackStackEntry> entries, https://developer.android.com/reference/androidx/navigation/NavOptions navOptions, https://developer.android.com/reference/androidx/navigation/Navigator.Extras navigatorExtras )` {@inheritDoc} |
| `void` | `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#onAttach(androidx.navigation.NavigatorState)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/navigation/NavigatorState state)` |
| `void` | `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#onLaunchSingleTop(androidx.navigation.NavBackStackEntry)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/navigation/NavBackStackEntry backStackEntry)` {@inheritDoc} |
| `void` | `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#onRestoreState(android.os.Bundle)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/os/Bundle.html savedState)` |
| `https://developer.android.com/reference/android/os/Bundle.html` | `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#onSaveState()()` |
| `void` | `https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#popBackStack(androidx.navigation.NavBackStackEntry,kotlin.Boolean)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/navigation/NavBackStackEntry popUpTo, boolean savedState)` {@inheritDoc} |

### Inherited methods

From [androidx.navigation.Navigator](https://developer.android.com/reference/androidx/navigation/Navigator) `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/navigation/NavigatorState` `https://developer.android.com/reference/androidx/navigation/Navigator#getState()()` `final boolean` `https://developer.android.com/reference/androidx/navigation/Navigator#isAttached()()` `https://developer.android.com/reference/androidx/navigation/NavDestination` `https://developer.android.com/reference/androidx/navigation/Navigator#navigate(androidx.navigation.NavDestination,android.os.Bundle,androidx.navigation.NavOptions,androidx.navigation.Navigator.Extras)(
@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator.Destination destination,
https://developer.android.com/reference/android/os/Bundle.html args,
https://developer.android.com/reference/androidx/navigation/NavOptions navOptions,
https://developer.android.com/reference/androidx/navigation/Navigator.Extras navigatorExtras
)` `boolean` `https://developer.android.com/reference/androidx/navigation/Navigator#popBackStack()()`

## Public constructors

### FragmentNavigator

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/navigation#2.0.0)

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

## Public methods

### createDestination

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/navigation#1.0.0)

```
public @NonNull FragmentNavigator.Destination createDestination()
```

### instantiateFragment

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/navigation#2.0.0) Deprecated in [2.1.0](https://developer.android.com/jetpack/androidx/releases/navigation#2.1.0)

```
public @NonNull Fragment [instantiateFragment](https://developer.android.com/reference/androidx/navigation/fragment/FragmentNavigator#instantiateFragment(android.content.Context,androidx.fragment.app.FragmentManager,kotlin.String,android.os.Bundle))(
    @NonNull Context context,
    @NonNull FragmentManager fragmentManager,
    @NonNull String className,
    Bundle args
)
```

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

Instantiates the Fragment via the FragmentManager's `https://developer.android.com/reference/androidx/fragment/app/FragmentFactory`.

Note that this method is **not** responsible for calling `https://developer.android.com/reference/androidx/fragment/app/Fragment#setArguments(android.os.Bundle)` on the returned Fragment instance.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | Context providing the correct `https://developer.android.com/reference/java/lang/ClassLoader.html` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/FragmentManager fragmentManager` | FragmentManager the Fragment will be added to |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html className` | The Fragment to instantiate |
| `https://developer.android.com/reference/android/os/Bundle.html args` | The Fragment's arguments, if any |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment` | A new fragment instance. |

### navigate

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

{@inheritDoc}

This method should always call `https://developer.android.com/reference/androidx/fragment/app/FragmentTransaction#setPrimaryNavigationFragment(androidx.fragment.app.Fragment)` so that the Fragment associated with the new destination can be retrieved with `https://developer.android.com/reference/androidx/fragment/app/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 `https://developer.android.com/reference/androidx/fragment/app/FragmentTransaction#setPrimaryNavigationFragment(androidx.fragment.app.Fragment)` so that the Fragment associated with the new destination can be retrieved with `https://developer.android.com/reference/androidx/fragment/app/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 `https://developer.android.com/reference/androidx/fragment/app/FragmentTransaction#setPrimaryNavigationFragment(androidx.fragment.app.Fragment)` if the pop succeeded so that the newly visible Fragment can be retrieved with `https://developer.android.com/reference/androidx/fragment/app/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.