public static void navigateUp(Activity activity, Bundle extras) { Intent upIntent = NavUtils.getParentActivityIntent(activity); if (upIntent != null) { if (extras != null) { upIntent.putExtras(extras); } if (NavUtils.shouldUpRecreateTask(activity, upIntent)) { // This activity is NOT part of this app's task, so create a new task // when navigating up, with a synthesized back stack. TaskStackBuilder.create(activity) // Add all of this activity's parents to the back stack. .addNextIntentWithParentStack(upIntent) // Navigate up to the closest parent. .startActivities(); } else { // This activity is part of this app's task, so simply // navigate up to the logical parent activity. // According to http://stackoverflow.com/a/14792752/2420519 //NavUtils.navigateUpTo(activity, upIntent); upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); activity.startActivity(upIntent); } } activity.finish(); }
public static boolean performFinishActivity(Activity activity, boolean forceNavigateUp) { if (activity == null) { return false; } boolean hasParent = activity.getIntent().getBooleanExtra(Constants.EXTRA_HAS_PARENT, false); if (forceNavigateUp || !hasParent) { Intent upIntent = NavUtils.getParentActivityIntent(activity); if (upIntent != null) { if (!hasParent) { upIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); } if (NavUtils.shouldUpRecreateTask(activity, upIntent)) { TaskStackBuilder.create(activity) .addNextIntentWithParentStack(upIntent) .startActivities(); } else { activity.startActivity(upIntent); } } } activity.finish(); return true; }
void navigateUp() { // Navigate up to the track associated with this event Intent upIntent = new Intent(this, TrackScheduleActivity.class); upIntent.putExtra(TrackScheduleActivity.EXTRA_DAY, event.getDay()); upIntent.putExtra(TrackScheduleActivity.EXTRA_TRACK, event.getTrack()); upIntent.putExtra(TrackScheduleActivity.EXTRA_FROM_EVENT_ID, event.getId()); if (NavUtils.shouldUpRecreateTask(this, upIntent)) { TaskStackBuilder.create(this) .addNextIntentWithParentStack(upIntent) .startActivities(); finish(); } else { // Replicate the compatibility implementation of NavUtils.navigateUpTo() // to ensure the parent Activity is always launched // even if not present on the back stack. upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(upIntent); finish(); } }