Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using support action bar home enabled

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.

like image 639
Barry Irvine Avatar asked Oct 10 '13 09:10

Barry Irvine


1 Answers

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.

like image 113
Jason Saruulo Avatar answered Oct 05 '22 06:10

Jason Saruulo