# NavUtils

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/app/NavUtils.java+class:androidx.core.app.NavUtils) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***

<br />

```
public final class NavUtils
```

<br />

*** ** * ** ***

NavUtils provides helper functionality for applications implementing recommended Android UI navigation patterns. For information about recommended navigation patterns see [Tasks and Back Stack](https://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack) from the developer guide and [Navigation](https://developer.android.com/design/patterns/navigation) from the design guide.

## Summary

| ### Constants |
|---|---|
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY() = "android.support.PARENT_ACTIVITY"` |

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/Intent.html` | `https://developer.android.com/reference/androidx/core/app/NavUtils#getParentActivityIntent(android.app.Activity)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity)` Obtain an `https://developer.android.com/reference/android/content/Intent.html` that will launch an explicit target activity specified by sourceActivity's `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element in the application's manifest. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/Intent.html` | `https://developer.android.com/reference/androidx/core/app/NavUtils#getParentActivityIntent(android.content.Context,android.content.ComponentName)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/ComponentName.html componentName )` Obtain an `https://developer.android.com/reference/android/content/Intent.html` that will launch an explicit target activity specified by sourceActivityClass's `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element in the application's manifest. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/Intent.html` | `https://developer.android.com/reference/androidx/core/app/NavUtils#getParentActivityIntent(android.content.Context,java.lang.Class<?>)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> sourceActivityClass )` Obtain an `https://developer.android.com/reference/android/content/Intent.html` that will launch an explicit target activity specified by sourceActivityClass's `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element in the application's manifest. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/app/NavUtils#getParentActivityName(android.app.Activity)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity)` Return the fully qualified class name of sourceActivity's parent activity as specified by a `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element within the activity element in the application's manifest. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/app/NavUtils#getParentActivityName(android.content.Context,android.content.ComponentName)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/ComponentName.html componentName )` Return the fully qualified class name of a source activity's parent activity as specified by a `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element within the activity element in the application's manifest. |
| `static void` | `https://developer.android.com/reference/androidx/core/app/NavUtils#navigateUpFromSameTask(android.app.Activity)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity)` Convenience method that is equivalent to calling `` `https://developer.android.com/reference/androidx/core/app/NavUtils#navigateUpTo(android.app.Activity,android.content.Intent)`(sourceActivity, `https://developer.android.com/reference/androidx/core/app/NavUtils#getParentActivityIntent(android.app.Activity)` (sourceActivity)) ``. sourceActivity will be finished by this call. |
| `static void` | `@https://developer.android.com/reference/androidx/annotation/ReplaceWith(expression = "sourceActivity.navigateUpTo(upIntent)") [navigateUpTo](https://developer.android.com/reference/androidx/core/app/NavUtils#navigateUpTo(android.app.Activity,android.content.Intent))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html upIntent)` **This method is deprecated.** Call navigateUpTo directly. <br /> |
| `static boolean` | `@https://developer.android.com/reference/androidx/annotation/ReplaceWith(expression = "sourceActivity.shouldUpRecreateTask(targetIntent)") [shouldUpRecreateTask](https://developer.android.com/reference/androidx/core/app/NavUtils#shouldUpRecreateTask(android.app.Activity,android.content.Intent))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html targetIntent )` **This method is deprecated.** Call shouldUpRecreateTask directly. <br /> |

## Constants

### PARENT_ACTIVITY

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

```
public static final String PARENT_ACTIVITY = "android.support.PARENT_ACTIVITY"
```

## Public methods

### getParentActivityIntent

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

```
public static @Nullable Intent getParentActivityIntent(@NonNull Activity sourceActivity)
```

Obtain an `https://developer.android.com/reference/android/content/Intent.html` that will launch an explicit target activity specified by sourceActivity's `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element in the application's manifest. The android:parentActivityName attribute will be preferred if it is present.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity` | Activity to fetch a parent intent for |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/Intent.html` | a new Intent targeting the defined parent activity of sourceActivity |

### getParentActivityIntent

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

```
public static @Nullable Intent getParentActivityIntent(
    @NonNull Context context,
    @NonNull ComponentName componentName
)
```

Obtain an `https://developer.android.com/reference/android/content/Intent.html` that will launch an explicit target activity specified by sourceActivityClass's `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element in the application's manifest.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | Context for looking up the activity component for the source activity |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/ComponentName.html componentName` | ComponentName for the source Activity |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/Intent.html` | a new Intent targeting the defined parent activity of sourceActivity |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/pm/PackageManager.NameNotFoundException.html` | if the ComponentName for sourceActivityClass is invalid |

### getParentActivityIntent

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

```
public static @Nullable Intent getParentActivityIntent(
    @NonNull Context context,
    @NonNull Class<@NonNull ?> sourceActivityClass
)
```

Obtain an `https://developer.android.com/reference/android/content/Intent.html` that will launch an explicit target activity specified by sourceActivityClass's `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element in the application's manifest.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | Context for looking up the activity component for sourceActivityClass |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> sourceActivityClass` | `https://developer.android.com/reference/java/lang/Class.html` object for an Activity class |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/Intent.html` | a new Intent targeting the defined parent activity of sourceActivity |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/pm/PackageManager.NameNotFoundException.html` | if the ComponentName for sourceActivityClass is invalid |

### getParentActivityName

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

```
public static @Nullable String getParentActivityName(@NonNull Activity sourceActivity)
```

Return the fully qualified class name of sourceActivity's parent activity as specified by a `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element within the activity element in the application's manifest.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity` | Activity to fetch a parent class name for |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | The fully qualified class name of sourceActivity's parent activity or null if it was not specified |

### getParentActivityName

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

```
public static @Nullable String getParentActivityName(
    @NonNull Context context,
    @NonNull ComponentName componentName
)
```

Return the fully qualified class name of a source activity's parent activity as specified by a `https://developer.android.com/reference/androidx/core/app/NavUtils#PARENT_ACTIVITY()` element within the activity element in the application's manifest. The source activity is provided by componentName.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | Context for looking up the activity component for the source activity |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/ComponentName.html componentName` | ComponentName for the source Activity |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | The fully qualified class name of sourceActivity's parent activity or null if it was not specified |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/pm/PackageManager.NameNotFoundException.html` |   |

### navigateUpFromSameTask

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

```
public static void navigateUpFromSameTask(@NonNull Activity sourceActivity)
```

Convenience method that is equivalent to calling `` `https://developer.android.com/reference/androidx/core/app/NavUtils#navigateUpTo(android.app.Activity,android.content.Intent)`(sourceActivity, `https://developer.android.com/reference/androidx/core/app/NavUtils#getParentActivityIntent(android.app.Activity)` (sourceActivity)) ``. sourceActivity will be finished by this call.

*Note:* This method should only be used when sourceActivity and the corresponding parent are within the same task. If up navigation should cross tasks in some cases, see `https://developer.android.com/reference/androidx/core/app/NavUtils#shouldUpRecreateTask(android.app.Activity,android.content.Intent)`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity` | The current activity from which the user is attempting to navigate up |

### navigateUpTo

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.15.0](https://developer.android.com/jetpack/androidx/releases/core#1.15.0)

```
@ReplaceWith(expression = "sourceActivity.navigateUpTo(upIntent)")
public static void [navigateUpTo](https://developer.android.com/reference/androidx/core/app/NavUtils#navigateUpTo(android.app.Activity,android.content.Intent))(@NonNull Activity sourceActivity, @NonNull Intent upIntent)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Call navigateUpTo directly.

Navigate from sourceActivity to the activity specified by upIntent, finishing sourceActivity in the process. upIntent will have the flag `https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP` set by this method, along with any others required for proper up navigation as outlined in the Android Design Guide.

This method should be used when performing up navigation from within the same task as the destination. If up navigation should cross tasks in some cases, see `https://developer.android.com/reference/androidx/core/app/NavUtils#shouldUpRecreateTask(android.app.Activity,android.content.Intent)`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity` | The current activity from which the user is attempting to navigate up |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html upIntent` | An intent representing the target destination for up navigation |

### shouldUpRecreateTask

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.15.0](https://developer.android.com/jetpack/androidx/releases/core#1.15.0)

```
@ReplaceWith(expression = "sourceActivity.shouldUpRecreateTask(targetIntent)")
public static boolean [shouldUpRecreateTask](https://developer.android.com/reference/androidx/core/app/NavUtils#shouldUpRecreateTask(android.app.Activity,android.content.Intent))(
    @NonNull Activity sourceActivity,
    @NonNull Intent targetIntent
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Call shouldUpRecreateTask directly.

Returns true if sourceActivity should recreate the task when navigating 'up' by using targetIntent.

If this method returns false the app can trivially call `https://developer.android.com/reference/androidx/core/app/NavUtils#navigateUpTo(android.app.Activity,android.content.Intent)` using the same parameters to correctly perform up navigation. If this method returns true, the app should synthesize a new task stack by using `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` or another similar mechanism to perform up navigation.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity` | The current activity from which the user is attempting to navigate up |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html targetIntent` | An intent representing the target destination for up navigation |

| Returns |
|---|---|
| `boolean` | true if navigating up should recreate a new task stack, false if the same task should be used for the destination |