Java Code Examples for android.support.v4.app.NavUtils#getParentActivityIntent()

The following examples show how to use android.support.v4.app.NavUtils#getParentActivityIntent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FeedActivity.java    From android-mvp-architecture with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: ManageReposActivity.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_add_repo:
            showAddRepo();
            return true;
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) {
                TaskStackBuilder.create(this)
                        .addNextIntentWithParentStack(upIntent)
                        .startActivities();
            } else {
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 3
Source File: UserActivity.java    From v2ex-daily-android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if(NavUtils.shouldUpRecreateTask(this, upIntent)){
                TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
            }else{
                upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 4
Source File: TopicActivity.java    From v2ex-daily-android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if(NavUtils.shouldUpRecreateTask(this, upIntent)){
                TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
            }else{
                upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 5
Source File: MapActivity.java    From android-map_list with MIT License 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);

            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                TaskStackBuilder.create(this)
                        .addNextIntentWithParentStack(upIntent)
                        .startActivities();
            } else {
                NavUtils.navigateUpTo(this, upIntent);
            }

            return true;
    }

    return super.onOptionsItemSelected(item);
}
 
Example 6
Source File: ChatActivity.java    From weixin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case android.R.id.home://返回上一菜单页
		AppToast.getToast().show("返回上一页");
		Intent upIntent = NavUtils.getParentActivityIntent(this);
		if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
			TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
		} else {
			upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			NavUtils.navigateUpTo(this, upIntent);
		}
		break;
	case R.id.menu_chat_chatInfo:
		AppToast.getToast().show(R.string.text_menu_chatInfo);
		break;
	default:
		break;
	}
	return super.onOptionsItemSelected(item);
}
 
Example 7
Source File: DebatesActivity.java    From kaif-android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
    case android.R.id.home:
      Intent upIntent = NavUtils.getParentActivityIntent(this);
      if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
        TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
      } else {
        upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        NavUtils.navigateUpTo(this, upIntent);
      }
      return true;
    case R.id.action_open_link:
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(article.getPermaLink());
      intent.addCategory(Intent.CATEGORY_BROWSABLE);
      this.startActivity(intent);
      return true;
  }

  return super.onOptionsItemSelected(item);
}
 
Example 8
Source File: SettingsActivity.java    From Readily with MIT License 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
		case android.R.id.home:
			Intent upIntent = NavUtils.getParentActivityIntent(this);
			if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
				TaskStackBuilder.create(this)
						.addNextIntentWithParentStack(upIntent)
						.startActivities();
			} else {
				upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(upIntent);
				finish();
			}
			return true;
	}
	return super.onOptionsItemSelected(item);
}
 
Example 9
Source File: SettingsActivity.java    From android with MIT License 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        Intent upIntent = NavUtils.getParentActivityIntent(this);
        if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) {
            // This activity is NOT part of this app's task, so create a new task
            // when navigating up, with a synthesized back stack.
            // This is probably because we opened settings from the notification.
            TaskStackBuilder.create(this)
                    // Add all of this activity's parents to the back stack
                    .addNextIntentWithParentStack(upIntent)
                    // Navigate up to the closest parent
                    .startActivities();
        } else {
            NavUtils.navigateUpTo(this, upIntent);
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 10
Source File: RegistActivity.java    From weixin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case android.R.id.home://返回上一菜单页
		AppToast.getToast().show("返回上一页");
		Intent upIntent = NavUtils.getParentActivityIntent(this);
		if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
			TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
		} else {
			upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			NavUtils.navigateUpTo(this, upIntent);
		}
		break;

	default:
		break;
	}
	return super.onOptionsItemSelected(item);
}
 
Example 11
Source File: DetailActivity.java    From masterdetail-j2objc-swift with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {

        final EditText title = (EditText) findViewById(R.id.masterdetail_detail_title);
        final EditText words = (EditText) findViewById(R.id.masterdetail_detail_words);

        viewModel.save(title.getText().toString(), words.getText().toString());

        Intent upIntent = NavUtils.getParentActivityIntent(this);
        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);
}
 
Example 12
Source File: BaseActivity.java    From RetailStore with Apache License 2.0 5 votes vote down vote up
/**
 * This utility method handles Up navigation intents by searching for a parent activity and
 * navigating there if defined. When using this for an activity make sure to define both the
 * native parentActivity as well as the AppCompat one when supporting API levels less than 16.
 * when the activity has a single parent activity. If the activity doesn't have a single parent
 * activity then don't define one and this method will use back button functionality. If "Up"
 * functionality is still desired for activities without parents then use
 * {@code syntheticParentActivity} to define one dynamically.
 * <p>
 * Note: Up navigation intents are represented by a back arrow in the top left of the Toolbar
 * in Material Design guidelines.
 *
 * @param currentActivity         Activity in use when navigate Up action occurred.
 * @param syntheticParentActivity Parent activity to use when one is not already configured.
 */
public static void navigateUpOrBack(Activity currentActivity,
                                    Class<? extends Activity> syntheticParentActivity) {
    // Retrieve parent activity from AndroidManifest.
    Intent intent = NavUtils.getParentActivityIntent(currentActivity);

    // Synthesize the parent activity when a natural one doesn't exist.
    if (intent == null && syntheticParentActivity != null) {
        try {
            intent = NavUtils.getParentActivityIntent(currentActivity, syntheticParentActivity);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    if (intent == null) {
        // No parent defined in manifest. This indicates the activity may be used by
        // in multiple flows throughout the app and doesn't have a strict parent. In
        // this case the navigation up button should act in the same manner as the
        // back button. This will result in users being forwarded back to other
        // applications if currentActivity was invoked from another application.
        currentActivity.onBackPressed();
    } else {
        if (NavUtils.shouldUpRecreateTask(currentActivity, intent)) {
            // Need to synthesize a backstack since currentActivity was probably invoked by a
            // different app. The preserves the "Up" functionality within the app according to
            // the activity hierarchy defined in AndroidManifest.xml via parentActivity
            // attributes.
            TaskStackBuilder builder = TaskStackBuilder.create(currentActivity);
            builder.addNextIntentWithParentStack(intent);
            builder.startActivities();
        } else {
            // Navigate normally to the manifest defined "Up" activity.
            NavUtils.navigateUpTo(currentActivity, intent);
        }
    }
}
 
Example 13
Source File: NewsFeedActivity.java    From kaif-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
    case android.R.id.home:
      Intent upIntent = NavUtils.getParentActivityIntent(this);
      if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
        TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
      } else {
        upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        NavUtils.navigateUpTo(this, upIntent);
      }
      return true;
  }
  return super.onOptionsItemSelected(item);
}
 
Example 14
Source File: AppViewActivity.java    From smartcard-reader with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent i = NavUtils.getParentActivityIntent(this);
            NavUtils.navigateUpTo(this, i);
            return true;

        case R.id.menu_edit_app:
            editApp();
            return true;

        case R.id.menu_copy_app:
            copyApp();
            return true;

        case R.id.menu_delete_app:
            showDialog(DIALOG_CONFIRM_DELETE);
            return true;

        case R.id.menu_select_app:
            selectApp();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 15
Source File: DetailFragment.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // Some small additions to handle "up" navigation correctly
            Intent upIntent = NavUtils.getParentActivityIntent(getActivity());
            upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

            // Check if up activity needs to be created (usually when
            // detail screen is opened from a notification or from the
            // Wearable app
            if (NavUtils.shouldUpRecreateTask(getActivity(), upIntent)
                    || getActivity().isTaskRoot()) {

                // Synthesize parent stack
                TaskStackBuilder.create(getActivity())
                        .addNextIntentWithParentStack(upIntent)
                        .startActivities();
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // On Lollipop+ we finish so to run the nice animation
                getActivity().finishAfterTransition();
                return true;
            }

            // Otherwise let the system handle navigating "up"
            return false;
        case R.id.map:
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(Constants.MAPS_INTENT_URI +
                    Uri.encode(mAttraction.name + ", " + mAttraction.city)));
            startActivity(intent);
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 16
Source File: NotifyActivity.java    From AndroidPlayground with MIT License 5 votes vote down vote up
@Override
public void onBackPressed() {
    Intent upIntent = NavUtils.getParentActivityIntent(this);
    if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) {
        TaskStackBuilder.create(this)
                .addNextIntent(upIntent)
                .startActivities();
    } else {
        NavUtils.navigateUpFromSameTask(this);
    }
}
 
Example 17
Source File: RepCallActivity.java    From android with MIT License 5 votes vote down vote up
private void returnToIssue() {
    if (isFinishing()) {
        return;
    }
    Intent upIntent = NavUtils.getParentActivityIntent(this);
    upIntent.putExtra(IssueActivity.KEY_ISSUE, mIssue);
    NavUtils.navigateUpTo(this, upIntent);
}
 
Example 18
Source File: BaseActivity.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
private void navigateToParent() {
    Intent intent = NavUtils.getParentActivityIntent(this);
    if (intent == null) {
        finish();
    } else {
        NavUtils.navigateUpFromSameTask(this);
    }
}
 
Example 19
Source File: SettingsActivity.java    From home-assistant-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent up = NavUtils.getParentActivityIntent(this);
            up.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            NavUtils.navigateUpTo(this, up);
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 20
Source File: BaseActivity.java    From EosCommander with MIT License 5 votes vote down vote up
@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 );
}