public class

NavUtils

extends Object

类概述


NavUtils为应用实现Android UI所推荐的导航模式提供了辅助功能.关于推荐导航模式的相关信息请参考开发者指南中的 Tasks and Back Stack 和设计指南中的 Navigation.

概要


Constants


public static final String PARENT_ACTIVITY

Public Methods


public static Intent getParentActivityIntent (Context context, Class<?> sourceActivityClass)

Obtain an [Intent](http://developer.android.com/reference/android/content/Intent.html) that will launch an explicit target activity specified by sourceActivityClass’s [PARENT_ACTIVITY](http://developer.android.com/reference/android/support/v4/app/NavUtils.html#PARENT_ACTIVITY) <meta-data> element in the application’s manifest.

Parameters
Returns
  • a new Intent targeting the defined parent activity of sourceActivity
Throws

public static Intent getParentActivityIntent (Context context, ComponentName componentName)

Obtain an [Intent](http://developer.android.com/reference/android/content/Intent.html) that will launch an explicit target activity specified by sourceActivityClass’s [PARENT_ACTIVITY](http://developer.android.com/reference/android/support/v4/app/NavUtils.html#PARENT_ACTIVITY) <meta-data> element in the application’s manifest.

Parameters
Returns
  • a new Intent targeting the defined parent activity of sourceActivity
Throws

public static Intent getParentActivityIntent (Activity sourceActivity)

Obtain an [Intent](http://developer.android.com/reference/android/content/Intent.html) that will launch an explicit target activity specified by sourceActivity’s [PARENT_ACTIVITY](http://developer.android.com/reference/android/support/v4/app/NavUtils.html#PARENT_ACTIVITY) <meta-data> element in the application’s manifest. If the device is running Jellybean or newer, the android:parentActivityName attribute will be preferred if it is present.

Parameters
Returns
  • a new Intent targeting the defined parent activity of sourceActivity

public static String getParentActivityName (Activity sourceActivity)

Return the fully qualified class name of sourceActivity’s parent activity as specified by a [PARENT_ACTIVITY](http://developer.android.com/reference/android/support/v4/app/NavUtils.html#PARENT_ACTIVITY) <meta-data> element within the activity element in the application’s manifest.

Parameters
Returns
  • The fully qualified class name of sourceActivity’s parent activity or null if it was not specified

public static String getParentActivityName (Context context, ComponentName componentName)

Return the fully qualified class name of a source activity’s parent activity as specified by a [PARENT_ACTIVITY](http://developer.android.com/reference/android/support/v4/app/NavUtils.html#PARENT_ACTIVITY) <meta-data> element within the activity element in the application’s manifest. The source activity is provided by componentName.

Parameters
Returns
  • The fully qualified class name of sourceActivity’s parent activity or null if it was not specified
Throws

public static void navigateUpFromSameTask (Activity sourceActivity)

Convenience method that is equivalent to calling <code>[navigateUpTo](http://developer.android.com/reference/android/support/v4/app/NavUtils.html#navigateUpTo(android.app.Activity, android.content.Intent))(sourceActivity, [getParentActivityIntent](http://developer.android.com/reference/android/support/v4/app/NavUtils.html#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 [shouldUpRecreateTask(Activity, Intent)](http://developer.android.com/reference/android/support/v4/app/NavUtils.html#shouldUpRecreateTask(android.app.Activity, android.content.Intent)).

Parameters

public static void navigateUpTo (Activity sourceActivity, Intent upIntent)

Navigate from sourceActivity to the activity specified by upIntent, finishing sourceActivity in the process. upIntent will have the flag [FLAG_ACTIVITY_CLEAR_TOP](http://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[shouldUpRecreateTask(Activity, Intent)](http://developer.android.com/reference/android/support/v4/app/NavUtils.html#shouldUpRecreateTask(android.app.Activity, android.content.Intent)).

Parameters

public static boolean shouldUpRecreateTask (Activity sourceActivity, Intent targetIntent)

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

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

Parameters
Returns
  • true if navigating up should recreate a new task stack, false if the same task should be used for the destination