26

I've set my action bar like so but nothing happens when I click the home up button. The two options below are enabled so shouldn't it go to the home activity automatically?

ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);

Action Bar

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    final String[] activities;
    Resources res = getResources();
    activities =  res.getStringArray(R.array.activities);

    ActionBar ab = getActionBar();
    ab.setHomeButtonEnabled(true);
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setTitle(R.string.app_name);
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    ab.show();

    /** Create an array adapter to populate dropdownlist */
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, activities);

    /** Setting dropdown items and item navigation listener for the actionbar */
    getActionBar().setListNavigationCallbacks(adapter, navigationListener);

    /** Enabling dropdown list navigation for the action bar */
    getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    return true;
}
1

6 Answers 6

66

As other people have said, the behaviour doesn't happen automatically - you need to tell it where to go.

However, I need to add another answer, as the current answers are all breaking Android design guidelines - Back != Home. See the documentation

What you really want to be doing is something along the lines of this:

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

Which will take you to the parent activity, rather than just go through the back stack. I've also added the Intent.Flag to clear the back stack, it's a useful one to have when going to a home activity and can stop the back stack getting in a muddle when your users are using the 'Up' button

6
  • Many thanks, this is what I was looking for, I assumed when set to true it would just return to the main class!
    – user1875797
    Commented Apr 22, 2013 at 15:29
  • 2
    I can't count the times a client has insisted the home button to function as back because it needs to be consistent with the flow of the iOS version of the app...
    – npace
    Commented Sep 10, 2014 at 7:09
  • 1
    It is not true, that generally Back != Home! In the exact same linked docs it says: Navigating to screens with multiple entry points: Sometimes a screen doesn't have a strict position within the app's hierarchy, and can be reached from multiple entry points—such as a settings screen that can be reached from any other screen in your app. In this case, the Up button should choose to return to the referring screen, behaving identically to Back. I believe this can be a quite common case... Commented Feb 3, 2015 at 14:04
  • @Till I would disagree. The very fact that Navigating to screens with multiple entry points is listed as a separate item indicates that it is a special case, not general behaviour. As such, overriding onBackPressed should be the exception (and yes, is occasionally valid) rather than the rule - but all of the other answers treat this as the common solution, hence me stressing this point in my answer. An app that commonly has screens without a strict position within the hierarchy would be hell to navigate. If this occurs often within your app, I would suggest redesigning your UI flow. Commented Feb 3, 2015 at 16:07
  • @MattTaylor What I meant to say is that I think it is a common case that an app needs to have that for some activity (e.g. a user profile that can be opened from any activity where users are being mentioned). Then still, this is the exception within the app, because, I agree, most navigation steps need to follow a strict path. But my point remains, that it is not true that this is always breaking Android design guidelines. Your answer suggests that whoever is implementing such a thing (even for just one case in an app), is doing something wrong, which is not necessarily true. Commented Feb 4, 2015 at 9:45
12

You also need to make sure your App knows what to do when it is pressed:

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    case android.R.id.home:
      // ProjectsActivity is my 'home' activity
      super. onBackPressed();
      return true;
    }
  return (super.onOptionsItemSelected(menuItem));
}
1
  • What is this special id android.R.id.home in context of android activities?
    – RBT
    Commented Aug 19, 2018 at 9:37
6

We have to define meta data into our child activity in AndroidManifast.xml file as per given in official document:

<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
    android:name="com.example.myfirstapp.MainActivity" ...>
    ...
</activity>
<!-- A child of the main activity -->
<activity
    android:name="com.example.myfirstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <!-- The meta-data element is needed for versions lower than 4.1 -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>

There is no need to define meta data if version is 4.1 or above and you have to enable your action bar home button as you have done in your code. No need to use back button code and it is working fine with my android app: Helper+

5

You need to define what happens here:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        onBackPressed();
    }
    return true;
}
5

You can achieve this using below one method although there are lots of ways to do so.

put this line inside your onCreate

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

override method onSupportNavigateUp in your activity

@Override
public boolean onSupportNavigateUp() {
    finish();
    return super.onSupportNavigateUp();
}
2

Add these lines in your code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case android.R.id.home:
        System.out.println("Pressed Back Button");
        break;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.