现在的位置: 首页 > 综合 > 正文

Android Support V7 包中 ActionBar的使用 (3) 为ActionBar添加Up导航功能

2013年08月31日 ⁄ 综合 ⁄ 共 3205字 ⁄ 字号 评论关闭

请首先参阅Android Support V7 包中 ActionBar的使用 (1)导入依赖工程

在很多应用里,我们都可以看到像上图所示的"<"图标,点击以后,可以返回到之前的页面。今天我们就来学习一下怎么实现这个效果。

一、应用场景

在界面A中有一个List,点击其中的一个item以后,进入到界面B,这时界面B就可以添加上图所示的Up导航功能

二、和按下后退键的区别

后退键是严格按照用户点击的时间顺序,来进行后退显示之前的屏幕。

而Up导航功能却和时间无关,只和程序的层级关系有关,也就是由你自己来决定Up点击后到达那个界面

三、实现方法:

1.在需要实现Up导航功能的界面中调用如下方法:

Actionbar actionBar = getSupportActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);

调用该方法以后,该界面的图标左侧就会出现“<”符,但是点击的时候是不会有任何效果。

2.实现Up导航功能

2.1>通过manifest文件实现

当parent Actitvity都一样的时候,推荐使用该方法

API>=16时,可以直接在activity节点添加parentActivityName属性

API<16时,还需要添加<meta-data>节点

示例代码:

<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 API level 7+ -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>

这样设置以后,当你点击“<”符的时候就会返回到指定的activity。

2.2> 通过代码实现

该方法适用于多个界面可以到达同一个界面,也就是说parent activity不一样的情况。

主要是重写getSupportParentActivityIntent() 和onCreateSupportNavigateUpTaskStack()方法.

子Activity运行在自己程序中的任务栈时,当用户点击“<”符,系统会调用getSupportParentActivityIntent()方法,我们需要重写这个方法来返回恰当的Intent。

子Activity运行在其他程序的任务栈时,当用户点击“<”符,系统会调用onCreateSupportNavigateUpTaskStack()方法,在该方法中要使用TaskStackBuilder来为其传递要返回的Activity。

如果不想重写onCreateSupportNavigateUpTaskStack()方法,也可以在manifest文件中像2.1介绍的方法一样添加父Activity。

另外需要注意的是,如果程序中的界面使用的不是Activity而是Fragement,那么上述的两个方法就无效了,而需要重写onSupportNavigateUp()方法和popBackStack()方法。


对于2.2,由于本人英文水平有限,理解的不是十分透彻,特此附上英文原文,大家参考

  • Or, override getSupportParentActivityIntent() and onCreateSupportNavigateUpTaskStack() in
    your activity
    .

    This is appropriate when the parent activity may be different depending on how the user arrived at the current screen. That is, if there are many paths that the user could have taken to reach the
    current screen, the Up button should navigate backward along the path the user actually followed to get there.

    The system calls getSupportParentActivityIntent() when
    the user presses the Up button while navigating your app (within your app's own task). If the activity that should open upon up navigation differs depending on how the user arrived at the current location, then you should override this method to return
    the Intentthat starts the appropriate
    parent activity.

    The system calls onCreateSupportNavigateUpTaskStack() for
    your activity when the user presses the Upbutton while your activity is running in a task that does not belong to your app. Thus, you must use theTaskStackBuilder passed
    to this method to construct the appropriate back stack that should be synthesized when the user navigates up.

    Even if you override getSupportParentActivityIntent() to
    specify up navigation as the user navigates your app, you can avoid the need to implement onCreateSupportNavigateUpTaskStack() by
    declaring "default" parent activities in the manifest file as shown above. Then the default implementation ofonCreateSupportNavigateUpTaskStack() will
    synthesize a back stack based on the parent activities declared in the manifest.

Note: If
you've built your app hierarchy using a series of fragments instead of multiple activities, then neither of the above options will work. Instead, to navigate up through your fragments, override
onSupportNavigateUp() to
perform the appropriate fragment transaction—usually by popping the current fragment from the back stack by calling 
popBackStack().


代码下载:

http://download.csdn.net/detail/hkg1pek/6018563



抱歉!评论已关闭.