diff --git a/docs/downloads/training/NavigationDrawer.zip b/docs/downloads/training/NavigationDrawer.zip new file mode 100644 index 0000000000000..3375a5c8987c1 Binary files /dev/null and b/docs/downloads/training/NavigationDrawer.zip differ diff --git a/docs/html/training/implementing-navigation/ancestral.jd b/docs/html/training/implementing-navigation/ancestral.jd index ac35e642ed17c..4e0b742afdb68 100644 --- a/docs/html/training/implementing-navigation/ancestral.jd +++ b/docs/html/training/implementing-navigation/ancestral.jd @@ -1,12 +1,6 @@ -page.title=Implementing Ancestral Navigation -parent.title=Implementing Effective Navigation -parent.link=index.html +page.title=Providing Up Navigation trainingnavtop=true -previous.title=Implementing Lateral Navigation -previous.link=lateral.html -next.title=Implementing Temporal Navigation -next.link=temporal.html @jd:body @@ -15,8 +9,9 @@ next.link=temporal.html

This lesson teaches you to:

    -
  1. Implement Up Navigation
  2. -
  3. Properly Handle the Application Home Screen
  4. +
  5. Specify the Parent Activity
  6. +
  7. Add Up Action
  8. +
  9. Navigate Up to Parent Activity

You should also read

@@ -38,87 +33,180 @@ next.link=temporal.html -

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.

+

All screens in your app that are not the main entrance to your app (the "home" screen) +should offer the user a way to navigate to the logical parent screen in the app's hierarchy by +pressing the Up button in the action bar. +This lesson shows you how to properly implement this behavior.

+ +
+

Up Navigation Design

+

The concepts and principles for Up navigation are described in Designing Effective +Navigation and the Navigation design +guide.

+
-

Implement Up Navigation

- -

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.

- - -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:

+ +

Specify the Parent Activity

+ +

To implement Up navigation, the first step is to declare which activity is the +appropriate parent for each activity. Doing so allows the system to facilitate navigation patterns +such as Up because the system can determine the logical parent activity from +the manifest file.

+ +

Beginning in Android 4.1 (API level 16), you can declare the logical parent of each +activity by specifying the {@code +android:parentActivityName} attribute +in the {@code <activity>} +element.

+ +

If your app supports Android 4.0 and lower, include the +Support Library with your app and +add a {@code <meta-data>} +element inside the {@code +<activity>}. Then specify the parent activity as the value +for {@code android.support.PARENT_ACTIVITY}, matching the {@code +android:parentActivityName} attribute.

+ +

For example:

+ +
+<application ... >
+    ...
+    <!-- The main/home activity (it has no parent activity) -->
+    <activity
+        android:name="com.example.myfirstapp.MainActivity" ...>
+        ...
+    </activity>
+    <!-- A child of the main activity -->
+    <activity
+        android:name="com.example.myfirstapp.DisplayMessageActivity"
+        android:label="@string/title_activity_display_message"
+        android:parentActivityName="com.example.myfirstapp.MainActivity" >
+        <!-- Parent activity meta-data to support 4.0 and lower -->
+        <meta-data
+            android:name="android.support.PARENT_ACTIVITY"
+            android:value="com.example.myfirstapp.MainActivity" />
+    </activity>
+</application>
+
+ +

With the parent activity declared this way, you can navigate Up +to the appropriate parent using the {@link android.support.v4.app.NavUtils} APIs, as shown in +the following sections.

+ + +

Add Up Action

+ +

To allow Up navigation with the app icon in the action bar, call +{@link android.app.ActionBar#setDisplayHomeAsUpEnabled setDisplayHomeAsUpEnabled()}:

 {@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.

+

This adds a left-facing caret alongside the app icon and enables it as an action button +such that when the user presses it, your activity receives a call to +{@link android.app.Activity#onOptionsItemSelected onOptionsItemSelected()}. The +ID for the action is {@code android.R.id.home}.

+ + + + + +

To navigate up when the user presses the app icon, you can use the {@link +android.support.v4.app.NavUtils} class's static method, +{@link android.support.v4.app.NavUtils#navigateUpFromSameTask +navigateUpFromSameTask()}. When you call this method, it finishes the current activity and +starts (or resumes) the appropriate parent activity. +If the target parent activity is in the task's back stack, it is brought +forward as defined by {@link android.content.Intent#FLAG_ACTIVITY_CLEAR_TOP}.

+ +

For example:

 {@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;
+    // Respond to the action bar's Up/Home button
+    case android.R.id.home:
+        NavUtils.navigateUpFromSameTask(this);
+        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.

+

However, using {@link android.support.v4.app.NavUtils#navigateUpFromSameTask +navigateUpFromSameTask()} is suitable only when your app is the owner of the current +task (that is, the user began this task from your app). If that's not true and your +activity was started in a task that belongs to a different app, then +navigating Up should create a new task that belongs to your app, which +requires that you create a new back stack.

-

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:

+ +

Navigate up with a new back stack

+ +

If your activity provides any intent filters +that allow other apps to start the +activity, you should implement the {@link android.app.Activity#onOptionsItemSelected +onOptionsItemSelected()} callback such that if the user presses the Up button +after entering your activity from another app's task, your app starts a new task +with the appropriate back stack before navigating up.

+ +

You can do so by first calling +{@link android.support.v4.app.NavUtils#shouldUpRecreateTask shouldUpRecreateTask()} to check +whether the current activity instance exists in a different app's task. If +it returns true, then build a new task with {@link android.support.v4.app.TaskStackBuilder}. +Otherwise, you can use the {@link android.support.v4.app.NavUtils#navigateUpFromSameTask +navigateUpFromSameTask()} method as shown above.

+ +

For example:

 {@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;
+    // Respond to the action bar's Up/Home button
+    case android.R.id.home:
+        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);
 }
 
-

Properly Handle the Application Home Screen

- -

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);
-
+

Note: In order for the {@link +android.support.v4.app.TaskStackBuilder#addNextIntentWithParentStack addNextIntentWithParentStack()} +method to work, +you must declare the logical parent of each activity in your manifest file, using the +{@code +android:parentActivityName} attribute (and corresponding {@code <meta-data>} element) +as described above.

diff --git a/docs/html/training/implementing-navigation/index.jd b/docs/html/training/implementing-navigation/index.jd index 990bcfe7aecf1..e4421c640f540 100644 --- a/docs/html/training/implementing-navigation/index.jd +++ b/docs/html/training/implementing-navigation/index.jd @@ -1,5 +1,5 @@ page.title=Implementing Effective Navigation -page.tags="viewpager","tasks","back","up" +page.tags="viewpager","tasks","back","up","swipe view","drawer" trainingnavtop=true startpage=true @@ -12,9 +12,9 @@ startpage=true

Dependencies and prerequisites

@@ -40,28 +40,38 @@ startpage=true

This class demonstrates how to implement the key navigation design patterns detailed in the Designing Effective Navigation class. -The lessons in this class cover implementing navigation up, down, and across your application's screen -map.

+

-

After reading through the lessons in this class and exploring the associated sample application -(see right), you should also have a basic understanding of how to use -{@link android.app.ActionBar} and {@link android.support.v4.view.ViewPager}, two components that are fundamental to core app navigation.

+

After reading the lessons in this class, you should have a strong understanding of how to +implement navigation patterns with tabs, swipe views, and a navigation drawer. You should also +understand how to provide proper Up and Back navigation.

+ +

Note: Several elements of this class require the +Support Library APIs. +If you have not used the Support Library before, follow the lesson about Using the Support Library +to get your project set up.

Lessons

-
-
Implementing Lateral Navigation
-
Learn how to implement tabs and horizontal paging (swipe views).
+
Creating Swipe Views with Tabs
+
Learn how to implement tabs in the action bar and provide + horizontal paging (swipe views) to navigate between tabs.
-
Implementing Ancestral Navigation
-
Learn how to implement Up navigation.
+
Creating a Navigation Drawer
+
Learn how to build an interface with a hidden navigation drawer on the side + of the screen that opens with a swipe or by pressing the action bar's app icon.
-
Implementing Temporal Navigation
-
Learn how to correctly handle the Back button.
+
Providing Up Navigation
+
Learn how to implement Up navigation using the action bar's app icon.
+ +
Providing Proper Back Navigation
+
Learn how to correctly handle the Back button in special cases, + including how to insert activities into the back stack when deep-linking the user + from notifications or app widgets.
Implementing Descendant Navigation
-
Learn the finer points of implementing navigation into your application's information hierarchy.
+
Learn the finer points of navigating down into your application's information hierarchy.
diff --git a/docs/html/training/implementing-navigation/lateral.jd b/docs/html/training/implementing-navigation/lateral.jd index c8f57a20cce3e..4ab8fbd84fb94 100644 --- a/docs/html/training/implementing-navigation/lateral.jd +++ b/docs/html/training/implementing-navigation/lateral.jd @@ -1,10 +1,6 @@ -page.title=Implementing Lateral Navigation -parent.title=Implementing Effective Navigation -parent.link=index.html +page.title=Creating Swipe Views with Tabs trainingnavtop=true -next.title=Implementing Ancestral Navigation -next.link=ancestral.html @jd:body @@ -13,11 +9,13 @@ next.link=ancestral.html

This lesson teaches you to

    -
  1. Implement Tabs
  2. -
  3. Implement Horizontal Paging (Swipe Views)
  4. -
  5. Implement Swiping Between Tabs
  6. +
  7. Implement Swipe Views
  8. +
  9. Add Tabs to the Action Bar
  10. +
  11. Change Tabs with Swipe Views
  12. +
  13. Use a Title Strip Instead of Tabs
+

You should also read