NemesisDoom
NemesisDoom

Reputation: 596

Missing Back Button Material Theme

I'm using the Material Design (Android Support V7 AppCompat) and using the Support Toolbar instead of the ActionBar. But I've noticed that the Back Button it's gone, on previous versions of Android, when adding to the back stack a Fragment Transaction, automatically added the Back Button, now it's gone. The only way I found to show the Back Button, it's using an Activity and calling it.

Have someone found and resolved this?

Thanks!

EDIT

I'm using a DrawerLayout and an ActionBarDrawerToggle, now, I've used the answer that was given and used this:

private FragmentManager.OnBackStackChangedListener mOnBackStackChangedListener = new FragmentManager.OnBackStackChangedListener() {
    @Override
    public void onBackStackChanged() {
        boolean displayHomeAsUpEnabled = getSupportFragmentManager().getBackStackEntryCount() > 0;
        getSupportActionBar().setDisplayHomeAsUpEnabled(displayHomeAsUpEnabled);
    }
};

FragmentManager it's from android.support.v4. Now, when returning to the Home Screen, the 'Home' (Hamburger button) disappears. I now it's because "setDisplayHomeAsUpEnabled" it's on false... But how do I show the home button again?, on previous versions of Android, just adding the back stack and having the back stack to 0 were enough to show the Home Button and the Back Button.

Thanks

Upvotes: 0

Views: 1070

Answers (2)

NemesisDoom
NemesisDoom

Reputation: 596

Ok, so when using an ActionBarDrawerToggle, all I needed to do to show and disappear the back button was the following:

private FragmentManager.OnBackStackChangedListener mOnBackStackChangedListener = new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            boolean displayHomeAsUpEnabled = getSupportFragmentManager().getBackStackEntryCount() > 0;
            if(displayHomeAsUpEnabled){
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }else{
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                mDrawerToggle.syncState();
            }
        }
    };

Really Odd, because on Native Versions or Android Support V4 Library, I didn't need to do that to show the back button and disappearing it to reveal a Home Button,

Thank you guys!

Upvotes: 1

devops
devops

Reputation: 9197

Use ActionBar#setDisplayShowHomeEnabled

So you can enable the back Button like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //...

}

Upvotes: 0

Related Questions