Mr Pablo
Mr Pablo

Reputation: 4188

ActionBar Up Navigation not working on API 15

I have implemented the Up navigation for my ActionBar, but I'm having trouble with it on APi's lower than 16.

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.

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>

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.

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.+"
}

My Building.class has the following in onCreate:

getActionBar().setDisplayHomeAsUpEnabled(true);

I am under the impression my activity is fine not extending ActionBarActivity, as that is for APIs below 11, which I am not doing.

Upvotes: 1

Views: 1034

Answers (1)

user1922137
user1922137

Reputation:

Use this code:

@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);
}

With reference to the documentation:

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.

Upvotes: 5

Related Questions