Add back button to action bar

AndroidAndroid LayoutAndroid Actionbar

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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);
    }
}

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

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

Solution 5 - Android

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!

Solution 6 - Android

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);
  }
 }

Solution 7 - Android

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

Duplicate of https://stackoverflow.com/questions/11996664/android-sherlock-actionbar-up-button

Solution 8 - Android

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" >

Solution 9 - Android

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);
        }
    }

Solution 10 - Android

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);
}

Solution 11 - Android

Add this line in onCreate() method

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

then Override this method

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhaythem souissiView Question on Stackoverflow
Solution 1 - AndroidDaneshView Answer on Stackoverflow
Solution 2 - Androidclami219View Answer on Stackoverflow
Solution 3 - AndroidJason SaruuloView Answer on Stackoverflow
Solution 4 - AndroidBoschAlexView Answer on Stackoverflow
Solution 5 - AndroidVon IobroView Answer on Stackoverflow
Solution 6 - AndroidArunendraView Answer on Stackoverflow
Solution 7 - AndroidadvantejView Answer on Stackoverflow
Solution 8 - AndroidM. Usman KhanView Answer on Stackoverflow
Solution 9 - AndroidAmit VaghelaView Answer on Stackoverflow
Solution 10 - AndroidSaad MahmudView Answer on Stackoverflow
Solution 11 - AndroidTsanguuView Answer on Stackoverflow