kanytu / android-material-drawer-template

An Android template with navigation drawer for material design
Apache License 2.0
674 stars 217 forks source link

Nav drawer icon problem in fragment replace #40

Closed cj7 closed 9 years ago

cj7 commented 9 years ago

I was using replace the container with fragment. I want when replace the fragment, the nav drawer icon from hamburger change to back arrow, when user on back press, the nav icon turn back to hamburger. Below is my code.

public void setActionBarArrowDependingOnFragmentsBackStack() {
    int backStackEntryCount = getActivity().getSupportFragmentManager().getBackStackEntryCount();
 if(backStackEntryCount >= 1) {
       ((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
   }

It should turn back to hamburger icon when the backstackentrycount less than 1. But it not able to turn back. Is that i miss out any code? Thanks a lot if you can help me.

kanytu commented 9 years ago

Try this answer http://stackoverflow.com/a/26755373/3410697

You will need to call syncState as far as I know

cj7 commented 9 years ago

i have write the syncState inside the function. it was like

if (backStackEntryCount == 0) {
 mActionBarDrawerToggle.syncState();
}

It able to function when user click the 1st back button. But if user open onbackStack again, the setDisplayHomeAsUpEnabled was unable to work. It still display hamburger icon. Although the backStackEntryCount is more or equal to 1. It feel weird with the code just able work on 1st time.

kanytu commented 9 years ago

Please check if your backstack is really getting popped.

Also make sure you're calling ((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); before the syncstate because if you're setting it to true and never turn it back to false you will never get it right the second time you click.

I will create 2 methods to show back button and show drawer button so it can be simpler to other folks.

In the meanwhile check this answer too for a better explanation:

http://stackoverflow.com/a/26671641/3410697

pgarzina commented 8 years ago

thanks a million kanytu! i was not calling false on setDisplayHomeAsUpEnabled, was searching for about an hour and a half for the solution. Thanks man