25

I'm currently working on an Android app. I would like to use the app icon in the action bar to navigate to the "home" activity. I read on this page that all that needs to be done is to add an onOptionsItemSelected and look for the id android.R.id.home.

This is the code that I have implemented in my activity where I want to press the app icon to return to HomeActivity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case android.R.id.home:
        Intent intent = new Intent(this, HomeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

However, nothing happens. When debugging, I can see that clicking the icon doesn't trigger the onOptionsItemSelected() at all. Do I have to do something with the icon somewhere? As of now, it's all default, just this in the AndroidManifest.xml

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
JJD
  • 50,076
  • 60
  • 203
  • 339
Nait
  • 1,055
  • 1
  • 14
  • 19
  • I have only ever tried responding to the action bar icon in an activity that had an options menu. Temporarily add an options menu and see if that changes the behavior that you see. – CommonsWare Jan 21 '12 at 13:01
  • Also consider passing the flag [`FLAG_ACTIVITY_SINGLE_TOP` which avoids restarting the activity](http://stackoverflow.com/a/18658364/356895). – JJD Sep 06 '13 at 13:03
  • 2
    My issue was I was in `onOptionsItemSelected` I had `R.id.home` instead of `android.R.id.home` drove me nuts! – mbwasi Nov 01 '14 at 21:06

3 Answers3

38

For packages targetting API level 14 onwards, you need to enable the home button by calling setHomeButtonEnabled()

In your onCreate, add the following:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    getActionBar().setHomeButtonEnabled(true);
}
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
  • 4
    Also wanted to add this for those using ActionBarSherlock. `getSupportActionBar().setHomeButtonEnabled(true);` – euniceadu Mar 20 '13 at 17:45
4

If you use Android new support-actionbar (AppCompat) you need to make both calls.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    getActionBar().setHomeButtonEnabled(true);
}
getSupportActionBar().setHomeButtonEnabled(true);
Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
salcosand
  • 2,022
  • 4
  • 24
  • 27
0

i dont know if we have the same problem.

but, i was on that problem and now solved..

do you add

case android.R.id.home:
    Intent intent = new Intent(this, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    return true;

in HomeActivity ? this is false..

you should put that code on your secondActivity.. because your home button on secondActivity, not HomeActivity

case android.R.id.home:
     NavUtils.navigateUpFromSameTask(this);
     true;

hope this helps you

M Moersalin
  • 290
  • 3
  • 12