Source code
package android.support.v4.app;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build.VERSION;
import android.support.annotation.Nullable;
import android.support.v4.content.IntentCompat;
import android.util.Log;
public class NavUtils {
private static final NavUtilsImpl IMPL;
public static final String PARENT_ACTIVITY = "android.support.PARENT_ACTIVITY";
private static final String TAG = "NavUtils";
interface NavUtilsImpl {
Intent getParentActivityIntent(Activity activity);
String getParentActivityName(Context context, ActivityInfo activityInfo);
void navigateUpTo(Activity activity, Intent intent);
boolean shouldUpRecreateTask(Activity activity, Intent intent);
}
static class NavUtilsImplBase implements NavUtilsImpl {
NavUtilsImplBase() {
}
public Intent getParentActivityIntent(Activity activity) {
Intent intent = null;
String parentName = NavUtils.getParentActivityName(activity);
if (parentName != null) {
ComponentName target = new ComponentName(activity, parentName);
try {
intent = NavUtils.getParentActivityName(activity, target) == null ? IntentCompat.makeMainActivity(target) : new Intent().setComponent(target);
} catch (NameNotFoundException e) {
Log.e(NavUtils.TAG, "getParentActivityIntent: bad parentActivityName '" + parentName + "' in manifest");
}
}
return intent;
}
public boolean shouldUpRecreateTask(Activity activity, Intent targetIntent) {
String action = activity.getIntent().getAction();
return (action == null || action.equals("android.intent.action.MAIN")) ? false : true;
}
public void navigateUpTo(Activity activity, Intent upIntent) {
upIntent.addFlags(67108864);
activity.startActivity(upIntent);
activity.finish();
}
public String getParentActivityName(Context context, ActivityInfo info) {
if (info.metaData == null) {
return null;
}
String parentActivity = info.metaData.getString(NavUtils.PARENT_ACTIVITY);
if (parentActivity == null) {
return null;
}
if (parentActivity.charAt(0) == '.') {
return context.getPackageName() + parentActivity;
}
return parentActivity;
}
}
static class NavUtilsImplJB extends NavUtilsImplBase {
NavUtilsImplJB() {
}
public Intent getParentActivityIntent(Activity activity) {
Intent result = NavUtilsJB.getParentActivityIntent(activity);
if (result == null) {
return superGetParentActivityIntent(activity);
}
return result;
}
Intent superGetParentActivityIntent(Activity activity) {
return super.getParentActivityIntent(activity);
}
public boolean shouldUpRecreateTask(Activity activity, Intent targetIntent) {
return NavUtilsJB.shouldUpRecreateTask(activity, targetIntent);
}
public void navigateUpTo(Activity activity, Intent upIntent) {
NavUtilsJB.navigateUpTo(activity, upIntent);
}
public String getParentActivityName(Context context, ActivityInfo info) {
String result = NavUtilsJB.getParentActivityName(info);
if (result == null) {
return super.getParentActivityName(context, info);
}
return result;
}
}
static {
if (VERSION.SDK_INT >= 16) {
IMPL = new NavUtilsImplJB();
} else {
IMPL = new NavUtilsImplBase();
}
}
public static boolean shouldUpRecreateTask(Activity sourceActivity, Intent targetIntent) {
return IMPL.shouldUpRecreateTask(sourceActivity, targetIntent);
}
public static void navigateUpFromSameTask(Activity sourceActivity) {
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 void navigateUpTo(Activity sourceActivity, Intent upIntent) {
IMPL.navigateUpTo(sourceActivity, upIntent);
}
public static Intent getParentActivityIntent(Activity sourceActivity) {
return IMPL.getParentActivityIntent(sourceActivity);
}
public static Intent getParentActivityIntent(Context context, Class<?> sourceActivityClass) throws NameNotFoundException {
String parentActivity = getParentActivityName(context, new ComponentName(context, sourceActivityClass));
if (parentActivity == null) {
return null;
}
ComponentName target = new ComponentName(context, parentActivity);
return getParentActivityName(context, target) == null ? IntentCompat.makeMainActivity(target) : new Intent().setComponent(target);
}
public static Intent getParentActivityIntent(Context context, ComponentName componentName) throws NameNotFoundException {
String parentActivity = getParentActivityName(context, componentName);
if (parentActivity == null) {
return null;
}
ComponentName target = new ComponentName(componentName.getPackageName(), parentActivity);
return getParentActivityName(context, target) == null ? IntentCompat.makeMainActivity(target) : new Intent().setComponent(target);
}
@Nullable
public static String getParentActivityName(Activity sourceActivity) {
try {
return getParentActivityName(sourceActivity, sourceActivity.getComponentName());
} catch (NameNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
@Nullable
public static String getParentActivityName(Context context, ComponentName componentName) throws NameNotFoundException {
return IMPL.getParentActivityName(context, context.getPackageManager().getActivityInfo(componentName, 128));
}
private NavUtils() {
}
}