/** * Returns true if sourceActivity should recreate the task when navigating 'up' * by using targetIntent. * * <p>If this method returns false the app can trivially call * {@link #supportNavigateUpTo(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 {@link android.support.v4.app.TaskStackBuilder} or another similar mechanism to perform up navigation.</p> * * @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 */ public boolean supportShouldUpRecreateTask(Intent targetIntent) { return NavUtils.shouldUpRecreateTask(this, targetIntent); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: Intent upIntent = NavUtils.getParentActivityIntent(this); upIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); if (NavUtils.shouldUpRecreateTask(this, 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(this) // 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. NavUtils.navigateUpTo(this, upIntent); } return true; } return super.onOptionsItemSelected(item); }
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(); }
/** * Returns true if sourceActivity should recreate the task when navigating 'up' * by using targetIntent. * * <p>If this method returns false the app can trivially call * {@link #supportNavigateUpTo(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 {@link android.support.v4.app.TaskStackBuilder} or another similar mechanism to perform up navigation.</p> * * @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 */ public boolean supportShouldUpRecreateTask(@NonNull Intent targetIntent) { return NavUtils.shouldUpRecreateTask(this, targetIntent); }
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(); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This is called when the Home (Up) button is pressed in the action bar. // Create a simple intent that starts the hierarchical parent activity and // use NavUtils in the Support Package to ensure proper handling of Up. Intent upIntent = new Intent(this, MainActivity.class); if (NavUtils.shouldUpRecreateTask(this, upIntent)) { // This activity is not part of the application's task, so create a new task // with a synthesized back stack. TaskStackBuilder.from(this) // If there are ancestor activities, they should be added here. .addNextIntent(upIntent) .startActivities(); finish(); } else { // This activity is part of the application's task, so simply // navigate up to the hierarchical parent activity. NavUtils.navigateUpTo(this, upIntent); } return true; } return super.onOptionsItemSelected(item); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent upIntent = NavUtils.getParentActivityIntent(this); if ( null != upIntent ) { upIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); if (NavUtils.shouldUpRecreateTask(this, 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(this) // 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. NavUtils.navigateUpTo(this, upIntent); } } else { onBackPressed(); } return true; } return super.onOptionsItemSelected( item ); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: Intent upIntent = NavUtils.getParentActivityIntent(this); upIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); if (NavUtils.shouldUpRecreateTask(this, 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(this) // 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. NavUtils.navigateUpTo(this, upIntent); } return true; } return super.onOptionsItemSelected(item); }
private void goUp() { // Implementation of the up button from http://developer.android.com/training/implementing-navigation/ancestral.html // isTaskRoot() check works around a bug where pressing "up" does nothing when the viewer is launched // by tapping on a pdf download notification in the notification list. // see: http://stackoverflow.com/questions/19999619/navutils-navigateupto-does-not-start-any-activity Activity activity = getActivity(); Intent upIntent = NavUtils.getParentActivityIntent(activity); if (NavUtils.shouldUpRecreateTask(activity, upIntent) || activity.isTaskRoot()) { // e.g., this is the case when opening a pdf from the Downloads app and pressing the up button: // the ViewerActivity is running in the Downloads task, so the following will start a new task // to open the document-viewer library in. TaskStackBuilder.create(activity) .addNextIntentWithParentStack(upIntent) .startActivities(); getActivity().finish(); } else { // Restart the existing instance of the RecentActivity rather than starting a new one upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); NavUtils.navigateUpTo(activity, upIntent); } }
if (NavUtils.shouldUpRecreateTask(currentActivity, intent)) {