11
votes

I've just modified our code to use the new SupportActionBar provided in the v7-appcompat library but when running the code on a Jellybean phone (presumably the same problem exists for Honeycomb and Ice Cream Sandwich) the home button doesn't ever seem to be activated.

Calling getSupportActionBar().setHomeButtonEnabled(true); doesn't seem to do what it says but works for Gingerbread phones.

If I replace it with getActionBar().setHomeButtonEnabled(true) it does work.

The theme that I use for v11+ is as follows:

<style name="MyTheme" parent="@style/Theme.AppCompat">
    <item name="android:windowActionBar">true</item>
    <item name="android:windowNoTitle">false</item>
    <item name="android:listViewStyle">@style/MyListView</item>
    <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
    <item name="android:buttonStyle">@style/MyButton</item>
    <item name="android:radioButtonStyle">@style/MyRadioButtonStyle</item>
    <item name="android:windowContentOverlay">@drawable/ab_solid_dove_grey</item>
    <item name="android:windowTitleSize">@dimen/action_bar_height</item>
    <item name="android:selectableItemBackground">@drawable/sel_standard_item</item>
    <item name="android:windowBackground">@drawable/default_bg</item>
    <item name="android:actionMenuTextAppearance">@style/MyActionBarText</item>
    <item name="android:actionMenuTextColor">@color/gallery</item>
    <item name="android:tabWidgetStyle">@style/MyTabWidget</item>
</style>

And the action bar style v11+ is defined:

<style name="MyActionBarStyle" parent="android:style/Widget.Holo.ActionBar">
    <item name="android:displayOptions">useLogo|showHome|showCustom</item>
    <item name="displayOptions">useLogo|showHome|showCustom</item>
    <item name="android:actionBarSize">@dimen/action_bar_height</item>
    <item name="android:icon">@drawable/ic_launcher</item>
    <item name="android:background">@android:color/transparent</item> <!-- Remove blue line from bottom of action bar -->
</style>

Anyone know why the home button is not getting enabled when on an Android version that supports action bar correctly.

=== UPDATE === I've just looked at the source code for the appcompat library and I've noticed the following in ActionBarImplBase which looks wrong to me:

 setHomeButtonEnabled(abp.enableHomeButtonByDefault() || homeAsUp);

This means that the home button will only be enabled if the Android version is less than ICS or if I've enabled the up indicator? - which I don't want.

5
This was a bug (issuetracker.google.com/issues/36975994) in appcompat-v7 that was apparently resolved in support-v19 but now appears to have returned in androidx.appcompat.app.ActionBar.mike47

5 Answers

20
votes

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();
    // or call onBackPressed()
    return true;
}

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

15
votes

Have you tried using all three of these (also try swapping for getSupportActionbar())?

 getActionBar().setDisplayShowHomeEnabled(true);
 getActionBar().setHomeButtonEnabled(true);
 getActionBar().setDisplayHomeAsUpEnabled(true); 

Have you tried handling the button manually?

@Override
public boolean onOptionsItemSelected(int featureId, MenuItem item) {
     int itemId = item.getItemId();
     if(itemId == android.R.id.home){
         // Do stuff
     }
     return true;
}
1
votes

Try use Sherlock library for android devices such as Gingerbread cos android action bars is only supported from 3.0 upwards so the sherlock lock library gives you backward compatibility.

http://actionbarsherlock.com/ --- download library here.

Then add this lines in your code.

ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
         actionBar.setIcon(android.R.color.transparent);

        actionBar.setDisplayUseLogoEnabled(false);

This would help you to add a back home key in your action bar. It would also make your icon invisible if you dont want it to show. But if you want your app icon show on all activity simply comment this line below

actionBar.setIcon(android.R.color.transparent);

1
votes

Now Please try this. Cause I was able to solve my own problem like this though it was on Sherlock. From your styles above I can see you did some customization to your themes. In my case I did some customization to my Sherlock theme and this was what gave me problem cos on android 4.0 and above my theme failed. so I simple added a piece of code that tells android to use the default Sherlock theme when it is running on android 4.0 and greater. So I suppose this would work for you. you tell android to use the default theme of v7-appcompat library on the version of android that is not working for you.

Code is below:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            this.setTheme(R.style.Theme_Sherlock);
        } else {
            this.setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);

        }

In your case edit the theme to v7-appcompat library.

Please Mark as answer if it work for you. I believe it might be possible to customize the theme from the code for places were you are using this.

0
votes

You can add an ActionBar to your activity when running on API level 7 or higher by extending ActionBarActivity class for your activity and setting the activity theme to Theme.AppCompat or a similar theme.