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;
}
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
  • Check code in this link it will be helpful. [Link for code](http://stackoverflow.com/questions/11304483/proper-way-to-handle-action-bar-up-button/37692750#37692750) – Naeem Ibrahim Jun 08 '16 at 03:49

6 Answers6

65

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

Akshay
  • 814
  • 6
  • 19
Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
  • Many thanks, this is what I was looking for, I assumed when set to true it would just return to the main class! –  Apr 22 '13 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 Sep 10 '14 at 07: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... – Till - Appviewer.io Feb 03 '15 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. – Matt Taylor Feb 03 '15 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. – Till - Appviewer.io Feb 04 '15 at 09:45
  • make sure of using android.R.id.home not R.id.home – Mohammed Subhi Sheikh Quroush May 27 '15 at 13:38
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));
}
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
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+

Md Mohsin
  • 1,568
  • 1
  • 18
  • 28
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;
}
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
Heinrisch
  • 5,835
  • 4
  • 33
  • 43
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();
}
M.Usman
  • 2,049
  • 22
  • 25
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;
}
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
Anupam
  • 3,742
  • 18
  • 55
  • 87