64

I have been trying to add a back button to the action bar.

I want my view to look like this: enter image description here

I want to add the back button in the left of the action bar.

I added this code

ActionBar actionBar = getActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);

but it doesn't work.

How can I fix this?

4
  • 5
    Up button it not them same with back button. And i think add back button is anti Android pattern Commented Aug 22, 2012 at 10:18
  • 6
    The screenshot is of an iPhone. Android does not run on such devices. Android has its own back button, always available for the user; you do not need to put one in the action bar. Commented Aug 22, 2012 at 12:39
  • 3
    Many apps today (2014) put a back/up button in the actionbar (eg. Instagram) so I even if this is an antipattern.. It is clearly a need in users (maybe users going from iPhone see more benefit on this)
    – sports
    Commented Oct 6, 2014 at 18:42
  • please check this answer Commented Sep 26, 2016 at 11:58

11 Answers 11

128

After setting actionBar.setHomeButtonEnabled(true);

Add the following code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; goto parent activity.
            this.finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
4
  • 15
    That's a bit cavalier, you have to test that menuItem == android.R.id.home before taking that action (usually in a switch statement along with other options for that activity or fragment). For API >= 16, you can also simply define the parent activity in the manifest
    – RedGlyph
    Commented Sep 22, 2013 at 12:52
  • 1
    this crashed my android app
    – slier
    Commented Jan 12, 2015 at 16:58
  • @slier, my app crashed too, and I found that actionbar was returning null Commented Dec 3, 2015 at 6:22
  • 12
    use getSupportActionBar() instead
    – John
    Commented Dec 19, 2015 at 8:14
55

Add

actionBar.setHomeButtonEnabled(true);

and then add the following

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

As suggested by naXa I've added a check on the itemId, to have it work correctly in case there are multiple buttons on the action bar.

4
  • Where's a check that the selected option is Home button? Commented Dec 21, 2015 at 14:52
  • I'm not sure. You might ask a specific question about that! :)
    – clami219
    Commented Dec 21, 2015 at 22:40
  • I mean, if there are other options in menu (for example, Help) they will be handled by onOptionsItemSelected() too, so we need to check menuItem.getItemId() == android.R.id.home Commented Dec 22, 2015 at 5:48
  • You are right! Thanks for that... I'll fix immediately the code
    – clami219
    Commented Dec 22, 2015 at 10:24
27

this one worked for me:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your_activity);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // ... other stuff
}

@Override
public boolean onSupportNavigateUp(){
    finish();
    return true;
}

The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.

0
24

After setting

 actionBar.setHomeButtonEnabled(true);

You have to configure the parent activity in your AndroidManifest.xml

<activity
    android:name="com.example.MainActivity"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat" />
<activity
    android:name="com.example.SecondActivity"
    android:theme="@style/Theme.AppCompat" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.MainActivity" />
</activity>

Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html

2
  • 1
    This the cleaner route. Also, good link to implementing from android.
    – Kalel Wade
    Commented Feb 27, 2014 at 20:39
  • 1
    I suspect this will not work if your child activity has more than one parent activity. i.e. more than one activity has the ability to navigate to the child activity. Commented May 26, 2016 at 4:35
10

There are two ways to approach this.

Option 1: Update the Android Manifest If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar

<activity
    android:name=".SettingsActivity"
    android:label="Setting Activity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.example.MainActivity" />
</activity>

Option 2: Change a setting for the ActionBar If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your @imports match the level of compatibility you are looking for).

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_test);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);
}

Then, detect the 'back button' press and tell Android to close the currently open Activity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; goto parent activity.
            this.finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

That should do it!

7

Firstly Use this:

ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);

Then set operation of button click in onOptionsItemSelected method

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
         finish();
        return true;
     default:
        return super.onOptionsItemSelected(item);
  }
 }
4
  • is following should be there ? @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); }
    – Shrenik
    Commented Jul 24, 2015 at 13:27
  • If you need options in menu then it should be, otherwise not important.
    – Arunendra
    Commented Jul 27, 2015 at 6:11
  • I am using AppCompatActivity, and onOptionsItemSelected is there <br/> but the case case android.R.id.home: does not get called , any hint ?
    – Shrenik
    Commented Jul 28, 2015 at 6:00
  • All Methods are Not working in My Case Because i am using Fragment in Commented Feb 20, 2017 at 13:17
6

You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method

Duplicate of Android Sherlock ActionBar Up button

6

Simpler and better: For API >= 16

Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.

<activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
2
  • This doesn't work with me. It creates a back button on Actionbar and it goes to the parent Activity. But the parent Activity works nothing.
    – c-an
    Commented Dec 4, 2018 at 18:20
  • @c-an you can override the onOptionsItemSelected method and set onBackPressed for the itemId android.R.id.home Commented Jul 27, 2020 at 5:49
6

Use this to show back button and move to previous activity,

final ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.back_dark);


@Override
    public boolean onOptionsItemSelected(final MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
5

if anyone else need the solution

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home) {
        onBackPressed();
    }

    return super.onOptionsItemSelected(item);
}
1

Add this line in onCreate() method

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

then Override this method

 @Override
    public boolean onSupportNavigateUp(){
        finish();
        return true;
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.