Android Providing Up Navigation 提供向上的导航

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中描述了向上导航的概念和设计准则。

Figure 1.  The  Up  button in the action bar. 

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.
要实现向上导航,第一步就是为每一个activity声明合适的父activity。这么做可以使系统简化导航模式,例如向上导航,因为系统可以从manifest文件中判断它的逻辑父(logical parent)activity。

Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying theandroid:parentActivityName attribute in the <activity> element.
从Android 4.1 (API level 16)开始,你可以通过指定<activity>元素中的android:parentActivityName属性来声明每一个activity的逻辑父activity。
If your app supports Android 4.0 and lower, include the Support Library with your app and add a <meta-data>element inside the <activity>. Then specify the parent activity as the value forandroid.support.PARENT_ACTIVITY, matching the android:parentActivityName attribute.
如果你的app需要支持Android 4.0以下版本,在你的app中包含Support Library并添加<meta-data>元素到<activity>中。然后指定父activity的值为android.support.PARENT_ACTIVITY,并匹配android:parentActivityName的值。
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  NavUtils APIs, as shown in the following sections.
在父activity这样声明后,你可以使用NavUtils API进行向上导航操作,就像下一面这节。

Add Up Action

To allow  Up  navigation with the app icon in the action bar, call  setDisplayHomeAsUpEnabled() :
为使得向上操作在action bar上起作用,需要调用setDisplayHomeAsUpEnabled()方法
@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 .
这样,在app旁添加了一个左向符号,并用作操作按钮。当用户点击它时,你的activity会接收一个对 onOptionsItemSelected()的调用。操作的ID是android.R.id.home.



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.

在这么做之前,你可以先调用 shouldUpRecreateTask() 来检查当前的activity实例是否在另一个不同的app任务中。如果返回true,就使用 TaskStackBuilder 创建一个新任务。或者,你可以向上面那样使用 navigateUpFromSameTask() 方法。

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.

为了能使 addNextIntentWithParentStack() 方法起作用,你必须像上面说的那样,在你的manifest文件中使用 android:parentActivityName (和相应的 <meta-data> 元素)属性声明所有的activity的逻辑父activity。


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值