ActionBar Up导航不适用于API 15 [英] ActionBar Up Navigation not working on API 15
问题描述
我已经为ActionBar实现了Up导航,但是在APi低于16的APi上却遇到了麻烦.
I have implemented the Up navigation for my ActionBar, but I'm having trouble with it on APi's lower than 16.
在运行4.0.4(API 15)的测试设备上,我可以点击ActionBar中的向上"图标,但是它不会转到清单中定义的父活动.
On a test device, running 4.0.4 (API 15) I can tap the Up icon in the ActionBar, but it doesnt go to the parent activity, which I have defined in the manifest.
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.name" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="" />
<activity
android:name=".Home"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Buildings"
android:label="@string/title_activity_buildings"
android:parentActivityName=".Home"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Home" />
</activity>
<activity
android:name=".Map"
android:label="@string/title_activity_map"
android:parentActivityName=".Home"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Home" />
</activity>
<activity
android:name=".Timeline"
android:label="@string/title_activity_timeline"
android:parentActivityName=".Home"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Home" />
</activity>
<activity
android:name=".About"
android:label="@string/title_activity_about"
android:parentActivityName=".Home"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Home" />
</activity>
</application>
我了解到android:parentActivityName适用于API 16+,并且在运行4.4.4的另一台设备上运行良好.
I understand that android:parentActivityName is for API 16+, and that works fine on another device running 4.4.4.
我了解到我需要元数据标记,因此可以看到,我在其中添加了这些标记,但是根本无法正常工作.
I read that I needed the meta-data tags, so you can see, I added those in, but it simply won't work.
我还添加了对支持库v7的依赖.
I have added the dependency for support library v7 as well.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:6.1.+'
compile "com.android.support:appcompat-v7:21.0.+"
}
我的Building.class在onCreate中具有以下内容:
My Building.class has the following in onCreate:
getActionBar().setDisplayHomeAsUpEnabled(true);
我的印象是我的活动很好,不扩展了ActionBarActivity,因为这是针对11以下的API的,我没有这样做.
I am under the impression my activity is fine not extending ActionBarActivity, as that is for APIs below 11, which I am not doing.
推荐答案
使用以下代码:
@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);
}
参考文档:
当您在API 16+中使用android:parentActivityName
时,系统会读取此属性以确定当用户按下操作栏中的向上"按钮时应该启动哪个活动.但是,当使用支持库的meta-data
时,必须覆盖onOptionsItemSelected
,然后可以使用NavUtils
API导航到相应的父级.
when you use android:parentActivityName
in API 16+ the system reads this attribute to determine which activity should be started when the user presses the Up button in the action bar. But when you use meta-data
of support library, you have to override onOptionsItemSelected
then you can navigate Up to the appropriate parent using the NavUtils
APIs.
这篇关于ActionBar Up导航不适用于API 15的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!