page.title=Implementing Ancestral Navigation parent.title=Implementing Effective Navigation parent.link=index.html trainingnavtop=true previous.title=Implementing Lateral Navigation previous.link=lateral.html next.title=Implementing Temporal Navigation next.link=temporal.html @jd:body
EffectiveNavigation.zip
Ancestral navigation is up the application's information hierarchy, where the top of the hierarchy (or root) is the application's home screen. This navigation concept is described in Designing Effective Navigation. This lesson discusses how to provide ancestral navigation using the Up button in the action bar.
When implementing ancestral navigation, all screens in your application that aren't the home screen should offer a means of navigating to the immediate parent screen in the hierarchy via the Up button in the action bar.
Figure 1. The Up button in the action bar.
Regardless of how the current screen was reached, pressing this button should always take the user to the same screen in the hierarchy.
To implement Up, enable it in the action bar in your activity's {@link android.app.Activity#onCreate onCreate()} method:
{@literal @}Override public void onCreate(Bundle savedInstanceState) { ... getActionBar().setDisplayHomeAsUpEnabled(true); ... }
You should also handle android.R.id.home
in {@link android.app.Activity#onOptionsItemSelected onOptionsItemSelected()}. This resource is the menu item ID for the Home (or Up) button. To ensure that a specific parent activity is shown, DO NOT simply call {@link android.app.Activity#finish finish()}. Instead, use an intent such as the one described below.
{@literal @}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. Intent parentActivityIntent = new Intent(this, MyParentActivity.class); parentActivityIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(parentActivityIntent); finish(); return true; } return super.onOptionsItemSelected(item); }
When the current activity belongs to a task from a different application—for example if it was reached via an intent from another application—pressing Up should create a new task for the application with a synthesized back stack. This approach is described in Android Design: Navigation and the {@link android.support.v4.app.TaskStackBuilder} class reference.
The {@link android.support.v4.app.NavUtils} and {@link android.support.v4.app.TaskStackBuilder} classes in the Android Support Package provide helpers for implementing this behavior correctly. An example usage of these two helper classes is below:
{@literal @}Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent upIntent = new Intent(this, MyParentActivity.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) .addNextIntent(new Intent(this, MyGreatGrandParentActivity.class)) .addNextIntent(new Intent(this, MyGrandParentActivity.class)) .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); }
By default, the Home button in the action bar is interactive. Since it does not make much sense to navigate home—or up one level—while on the home screen, you should disable the button like so:
getActionBar().setHomeButtonEnabled(false);