Good day. I again have a question on the Drawer Layout, namely on the "Hamburger button". On one piece I have included the Navigation Drawer and the "Hamburger Button". When switching to the 2nd Fragment, I disable this button and turn on the Home Button:

toggle.setDrawerIndicatorEnabled(false); toggle.syncState(); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); setSupportActionBar(toolbar); 

When returning to the 1st Fragment, I disable the Home Button , and return the "Hamburger button." It appears, but stops responding. What is the problem? Here is the return code for the 1st fragment:

  setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(false); getSupportActionBar().setHomeButtonEnabled(false); toggle.setDrawerIndicatorEnabled(true); drawerLayout.setDrawerListener(toggle); toggle.syncState(); drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); 

If you return to add "Hamburger button" full initialization:

 toggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.setDrawerListener(toggle); toggle.syncState(); 

then the picture gets even weirder and more confusing. At the FIRST transition from the 1st fragment to the 2nd, in the 2nd fragment, the Home Button is shown correctly, when you return to the 1st fragment, the Navigation Drawer also works, but when you try to go AGAIN to the 2nd fragment, a Hamburger appears in TulBar button, instead of HomeButton'a (which was shown at the first transition). In general, I hope someone can understand my question and tell where the error is!

    2 answers 2

     drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); 

    Such commands are rude ... and it is better not to use them)

    This solution does not suit you?

      toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); 
    • Here the question is not how to go back, but on changing the buttons in the bar, re-read my question again, please. - ivanovd422
    • I think the problem with using this part of the code is "getSupportActionBar (). SetDisplayHomeAsUpEnabled (false); getSupportActionBar (). SetHomeButtonEnabled (false);" Therefore, the button does not work. false - iFr0z
    • You can of course make it easier to arrange the work of the fragments of the Activity and in each activity do onBackPressed (); then you will not be confused in such matters. I just wanted to do this kind of work with the fragments myself, but I realized that these are also dances with tambourines and translated the fragmentes into acitivity and I don’t have any misfortunes) - iFr0z
    • As far as I know, getSupportActionBar (). SetHomeButtonEnabled (false); does not affect the operation of the Hamurger Button. Although I rechecked, nothing has changed. Regarding the second comment, I have an asset, it has a fragment (1st). From the first fragment there is a transition to the second fragment (it does not refer to the activation). Transitions work, but when you return to the first fragment, the Hamburger Button stops working. Yes, the dances are still those. - ivanovd422

    Maybe someone will help. If instead of a standard toolbar button there is a hamburger with animation ( ActionBarDrawerToggle ), then switching the arrow-hamburger using ActionBar will not work adequately.

    Calling getSupportActionBar().setDisplayHomeAsUpEnabled(xxx); it works exactly 1 time. After returning to the hamburger using toggle.syncState(); The following call to setDisplayHomeAsUpEnabled(xxx) does not change the icon on the arrow.

    Solved the problem as follows:

    First I get an animated arrow from ActionBarDrawerToggle:

     DrawerArrowDrawable drawerArrow = toggle.getDrawerArrowDrawable(); 

    The animated arrow has setProgress (0-1.0f). Progress 0f is a hamburger, 1f is an arrow. So I switch to the arrow:

     drawerView.removeDrawerListener(toggle); drawerArrow.setProgress(1); backAvailable = true; 

    Here so back:

     drawerView.removeDrawerListener(toggle); drawerView.addDrawerListener(toggle); toggle.syncState(); backAvailable = false; 

    Well, respectively, I hang the listener to react to pressing:

     toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (backAvailable) { onBackPressed(); } else { if (drawerView.isDrawerOpen(layout_drawer)) { drawerView.closeDrawer(layout_drawer); } else { drawerView.openDrawer(layout_drawer); } } } });