http://developer.android.com/training/implementing-navigation/ancestral.html
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.
所有不是从主屏幕("home"屏幕)进入app的,都应该给用户提供一种方法,通过点击action bar中的Up按钮。可以回到app的结构层次中逻辑父屏幕。本课程向你说明如何正确地实现这一操作。
Up Navigation Design
The concepts and principles for Up navigation are described in Designing Effective Navigation and the Navigation design guide.
Designing Effective Navigation和the Navigation design guide中描述了向上导航的概念和设计准则。Specify the Parent Activity指定父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.android:parentActivityName
attribute in the <activity>
element.<activity>
元素中的android:parentActivityName属性来声明每一个activity的逻辑父activity。<meta-data>
element inside the <activity>
. Then specify the parent activity as the value forandroid.support.PARENT_ACTIVITY
, matching the android:parentActivityName
attribute.<meta-data>
元素到<activity>
中。然后指定父activity的值为android.support.PARENT_ACTIVITY
,并匹配android:parentActivityName的值。<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
NavUtils
APIs, as shown in the following sections.
Add Up Action
To allow Up navigation with the app icon in the action bar, callsetDisplayHomeAsUpEnabled()
:
@Override public void onCreate(Bundle savedInstanceState) { ... getActionBar().setDisplayHomeAsUpEnabled(true); }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
onOptionsItemSelected()
. The ID for the action is
android.R.id.home
.
onOptionsItemSelected()
. 的调用。操作的ID是android.R.id.home
.Navigate Up to Parent Activity
To navigate up when the user presses the app icon, you can use the NavUtils
class's static method,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 FLAG_ACTIVITY_CLEAR_TOP
.
要在用户点击app图标时向上导航,你可以使用NavUtils类中的静态方法navigateUpFromSameTask()。当你调用这一方法时,系统会结束当前的activity并启动(或恢复)相应的父activity。如果目标activity在任务的后退栈中(back stack),则目标activity会像FLAG_ACTIVITY_CLEAR_TOP定义的那样,提到栈顶。
For example:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); }However, using
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.
但是,只能是当你的app拥有当前任务(current task)(用户从你的app中发起这一任务)时navigateUpFromSameTask()才有用。如果你的activity是从别的app的任务中启动的话,向上导航操作就应该创建一个属于你的app的新任务,并需要你创建一个新的后退栈。
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 theonOptionsItemSelected()
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.
如果你的activity提供了任何允许被别的app启动的intent filters,那么你应该实现onOptionsItemSelected()回调,在用户从别的app任务进入你的activity后,点击Up按钮,在向上导航之前你的app用相应的后退栈开启一个新的任务。
You can do so by first calling 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 TaskStackBuilder
. Otherwise, you can use thenavigateUpFromSameTask()
method as shown above.
For example:
@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); 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); }
Note: In order for the addNextIntentWithParentStack()
method to work, you must declare the logical parent of each activity in your manifest file, using the android:parentActivityName
attribute (and corresponding <meta-data>
element) as described above.
<meta-data>
元素)属性声明所有的activity的逻辑父activity。