3

I'm currently trying to enable up navigation in my Android app. To do so, I followed the tutorial and implemented the necessary code into my activity:

SettingsActivity.java

package de.ntraum.arcon;

import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.MenuItem;

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setHomeButtonEnabled(true);
        addPreferencesFromResource(R.xml.settings);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            startActivity(new Intent(SettingsActivity.this, MainActivity.class)
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

}

If relevant, here's the AndroidManifest.xml and styles.xml:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.ntraum.arcon"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Light" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:exported="false"
            android:label="@string/title_activity_settings" >
            <intent-filter>
                <category android:name="android.intent.category.PREFERENCE" />
            </intent-filter>
        </activity>
    </application>

</manifest>

styles.xml

<resources>

    <style name="Light" parent="android:Theme.Holo.Light.DarkActionBar" />

    <style name="Dark" parent="android:Theme.Holo" />

</resources>

Issue

The thing is, when accessing the activity, the up indicator in the action bar is missing. Nevertheless, the navigation is actually working (it takes me back to the MainActivity.class), it's just the small indicator that does not show up correctly.

Screenshot of Activity with missing up indic

I've tested it on my own device and in the emulator so far.

nTraum
  • 1,426
  • 11
  • 14

2 Answers2

6

The docs say to call setDisplayHomeAsUpEnabled(true) on your action bar. You're actually calling a different method, setHomeButtonEnabled, so try adding the call from the docs.

wsanville
  • 37,158
  • 8
  • 76
  • 101
3

You can also set up the navigation "UP" functionality using XML in your AndroidManifest.xml instead of coding it programmatically. Very helpful to do it in one place if you have multiple activities all having UP navigation functionality.

<activity
            android:name=".SettingsActivity"
            android:exported="false"
            android:label="@string/title_activity_settings" 
            android:parentActivityName=".MainActivity" >
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
            <intent-filter>
                <category android:name="android.intent.category.PREFERENCE" />
            </intent-filter>
</activity>
Yaojin
  • 341
  • 2
  • 6