0

I have a AppCompatActivity activity named MainActivity with the following code placed on onCreate method to show/hide back and menu button

getSupportFragmentManager().addOnBackStackChangedListener(
    new FragmentManager.OnBackStackChangedListener() {

    @Override
    public void onBackStackChanged() {
        toggle.setDrawerIndicatorEnabled(
             getSupportFragmentManager().getBackStackEntryCount() == 0);

        getSupportActionBar().setDisplayHomeAsUpEnabled(
             getSupportFragmentManager().getBackStackEntryCount() > 0);
    }
});

This is my only activity, I use fragments for the different views. The back button shows perfectly when appropiated but does nothing when I click on it.

Do I have to put some code on fragments? I have checked many other similar questions but I'm not able to detect what's missed

EDIT

Many solutions ask to override onOptionsItemSelected on Fragment or Activity but this method is not called when I click on the back button on toolbar.

EDIT 2

If I comment line

toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);

then back button click opens navigation menu.

StackOverflower
  • 5,463
  • 13
  • 58
  • 89
  • Possible duplicate of [Switching between Android Navigation Drawer image and Up caret when using fragments](http://stackoverflow.com/questions/17258020/switching-between-android-navigation-drawer-image-and-up-caret-when-using-fragme) – Shabbir Dhangot Jan 12 '16 at 03:54
  • Do you write the code onBackPRessed() in MainActivity. – Pitty Jan 12 '16 at 04:31
  • Full solution here: http://stackoverflow.com/a/34026438/4409409 – Daniel Nugent Jan 12 '16 at 04:37

1 Answers1

0

You'll have to manually handle the home button as shown here : catch toolbar home button click event

then load the previous fragment from backstack: Get Fragment from backstack

Community
  • 1
  • 1
datalost
  • 3,765
  • 1
  • 25
  • 32