FileDocCategorySizeDatePackage
NavUtils.javaAPI DocAndroid 5.1 API13840Thu Mar 12 22:22:56 GMT 2015android.support.v4.app

NavUtils

public class NavUtils extends Object
NavUtils provides helper functionality for applications implementing recommended Android UI navigation patterns. For information about recommended navigation patterns see Tasks and Back Stack from the developer guide and Navigation from the design guide.

Fields Summary
private static final String
TAG
public static final String
PARENT_ACTIVITY
private static final NavUtilsImpl
IMPL
Constructors Summary
private NavUtils()
No instances!

    
Methods Summary
public static android.content.IntentgetParentActivityIntent(android.app.Activity sourceActivity)
Obtain an {@link Intent} that will launch an explicit target activity specified by sourceActivity's {@link #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.

param
sourceActivity Activity to fetch a parent intent for
return
a new Intent targeting the defined parent activity of sourceActivity

        return IMPL.getParentActivityIntent(sourceActivity);
    
public static android.content.IntentgetParentActivityIntent(android.content.Context context, java.lang.Class sourceActivityClass)
Obtain an {@link Intent} that will launch an explicit target activity specified by sourceActivityClass's {@link #PARENT_ACTIVITY} <meta-data> element in the application's manifest.

param
context Context for looking up the activity component for sourceActivityClass
param
sourceActivityClass {@link java.lang.Class} object for an Activity class
return
a new Intent targeting the defined parent activity of sourceActivity
throws
NameNotFoundException if the ComponentName for sourceActivityClass is invalid

        String parentActivity = getParentActivityName(context,
                new ComponentName(context, sourceActivityClass));
        if (parentActivity == null) return null;

        // If the parent itself has no parent, generate a main activity intent.
        final ComponentName target = new ComponentName(context, parentActivity);
        final String grandparent = getParentActivityName(context, target);
        final Intent parentIntent = grandparent == null
                ? IntentCompat.makeMainActivity(target)
                : new Intent().setComponent(target);
        return parentIntent;
    
public static android.content.IntentgetParentActivityIntent(android.content.Context context, android.content.ComponentName componentName)
Obtain an {@link Intent} that will launch an explicit target activity specified by sourceActivityClass's {@link #PARENT_ACTIVITY} <meta-data> element in the application's manifest.

param
context Context for looking up the activity component for the source activity
param
componentName ComponentName for the source Activity
return
a new Intent targeting the defined parent activity of sourceActivity
throws
NameNotFoundException if the ComponentName for sourceActivityClass is invalid

        String parentActivity = getParentActivityName(context, componentName);
        if (parentActivity == null) return null;

        // If the parent itself has no parent, generate a main activity intent.
        final ComponentName target = new ComponentName(
                componentName.getPackageName(), parentActivity);
        final String grandparent = getParentActivityName(context, target);
        final Intent parentIntent = grandparent == null
                ? IntentCompat.makeMainActivity(target)
                : new Intent().setComponent(target);
        return parentIntent;
    
public static java.lang.StringgetParentActivityName(android.app.Activity sourceActivity)
Return the fully qualified class name of sourceActivity's parent activity as specified by a {@link #PARENT_ACTIVITY} <meta-data> element within the activity element in the application's manifest.

param
sourceActivity Activity to fetch a parent class name for
return
The fully qualified class name of sourceActivity's parent activity or null if it was not specified

        try {
            return getParentActivityName(sourceActivity, sourceActivity.getComponentName());
        } catch (NameNotFoundException e) {
            // Component name of supplied activity does not exist...?
            throw new IllegalArgumentException(e);
        }
    
public static java.lang.StringgetParentActivityName(android.content.Context context, android.content.ComponentName componentName)
Return the fully qualified class name of a source activity's parent activity as specified by a {@link #PARENT_ACTIVITY} <meta-data> element within the activity element in the application's manifest. The source activity is provided by componentName.

param
context Context for looking up the activity component for the source activity
param
componentName ComponentName for the source Activity
return
The fully qualified class name of sourceActivity's parent activity or null if it was not specified

        PackageManager pm = context.getPackageManager();
        ActivityInfo info = pm.getActivityInfo(componentName, PackageManager.GET_META_DATA);
        String parentActivity = IMPL.getParentActivityName(context, info);
        return parentActivity;
    
public static voidnavigateUpFromSameTask(android.app.Activity sourceActivity)
Convenience method that is equivalent to calling {@link #navigateUpTo(Activity, Intent) navigateUpTo}(sourceActivity, {@link #getParentActivityIntent(Activity) getParentActivityIntent} (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 {@link #shouldUpRecreateTask(Activity, Intent)}.

param
sourceActivity The current activity from which the user is attempting to navigate up

        Intent upIntent = getParentActivityIntent(sourceActivity);

        if (upIntent == null) {
            throw new IllegalArgumentException("Activity " +
                    sourceActivity.getClass().getSimpleName() +
                    " does not have a parent activity name specified." +
                    " (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> " +
                    " element in your manifest?)");
        }

        navigateUpTo(sourceActivity, upIntent);
    
public static voidnavigateUpTo(android.app.Activity sourceActivity, android.content.Intent upIntent)
Navigate from sourceActivity to the activity specified by upIntent, finishing sourceActivity in the process. upIntent will have the flag {@link Intent#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 {@link #shouldUpRecreateTask(Activity, Intent)}.

param
sourceActivity The current activity from which the user is attempting to navigate up
param
upIntent An intent representing the target destination for up navigation

        IMPL.navigateUpTo(sourceActivity, upIntent);
    
public static booleanshouldUpRecreateTask(android.app.Activity sourceActivity, android.content.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 {@link #navigateUpTo(Activity, 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 {@link TaskStackBuilder} or another similar mechanism to perform up navigation.

param
sourceActivity The current activity from which the user is attempting to navigate up
param
targetIntent An intent representing the target destination for up navigation
return
true if navigating up should recreate a new task stack, false if the same task should be used for the destination

        final int version = android.os.Build.VERSION.SDK_INT;
        if (version >= 16) {
            IMPL = new NavUtilsImplJB();
        } else {
            IMPL = new NavUtilsImplBase();
        }
    
        return IMPL.shouldUpRecreateTask(sourceActivity, targetIntent);