Android Hamburger Menu and Back Arrow

7.3k Views Asked by At

I want to make on my MainActivity a property navigation for my users with hamburger and back arrow.

When I have only one fragment set hamburger, but if i have more fragments added on my MainActivity, set the back arrow.

How do I implement that?

Here is my implementation...

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private Toolbar toolbar;
private DrawerLayout drawer;
private NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setSupportActionBar(toolbar);

    final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView.setNavigationItemSelectedListener(this);

    //TODO: Insert back arrow button if have more than one fragment on backstack
    /*getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            int stackHeight = getSupportFragmentManager().getBackStackEntryCount();
            if (stackHeight > 0) {
                if (getSupportActionBar() != null) {
                    getSupportActionBar().setHomeButtonEnabled(true);
                    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                    toggle.setDrawerIndicatorEnabled(false);
                }
            } else {
                if (getSupportActionBar() != null) {
                    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                    toggle.setDrawerIndicatorEnabled(true);
                }
            }
        }
    });*/

    ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), new ListVeiculoFragment(), R.id.container_main);
}
}
5

There are 5 best solutions below

0
On

Work by creating Interface class:

public interface HideShowIconInterface{
    void showHamburgerIcon();
    void showBackIcon();
}

Implement Interface in your Activity:

public class YourActivity extends AppCompatActivity implements HideShowIconInterface{
    @Override
    public void showHamburgerIcon() {
       getSupportActionBar().setDisplayHomeAsUpEnabled(false);
       mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
    }
    @Override
    public void showBackIcon() {
      mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);   
   }
}

In your Fragment, call whatever you want by:

((HideShowIconInterface) getActivity()).showHamburgerIcon();

or

((HideShowIconInterface) getActivity()).showBackIcon();
0
On

You just add

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

For more details see Android - Switch ActionBar Back Button to Navigation Button.

0
On
 setSupportActionBar(toolbar);
    toolbar.setTitle(R.string.app_name);
    toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

put this code in your activity where you use humbarger style navigation it work for me.

2
On

You have to catch the moment when the numbers of the fragments change and use this code to hide (false) / show (true) the DrawerToggle "Hamburger"

  mDrawerToggle.setDrawerIndicatorEnabled(false);

EDIT:

In your Activity (somewhere) you have something like this (where you change current Fragment):

private void selectItem(int position) {

Fragment fragment = new MyFragment();

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.content_frame, fragment)
               .commit();
}

I would change last line by this:

fragmentManager.beginTransaction()
               .replace(R.id.content_frame, fragment)
               .addToBackStack(null)
               .commit();

and then check:

  if(fragmentManager.getBackStackEntryCount() > 1) mDrawerToggle.setDrawerIndicatorEnabled(false);
0
On

Actually none of above solutions works fully. There are couple of issues that have to be considered -

a) if using ActionBarDrawerToggle then getSupportActionBar().setDisplayHomeAsUpEnabled is now working as expected - it actually replaces icon for toggle, but clicks are still handled by toggle plus hamburger icon is hidden when toggle is enabled again

b) As fragments transaction (add, restore form backstack) are done asynchronously, check for actual backstack size has to be done after fragment transaction is done - e.g. from fragments onCreateView

Here is code that works for me (in Kotlin):

 override fun onCreate(savedInstanceState: Bundle?) {
    // ....
    drawerToggle  = ActionBarDrawerToggle(
            this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
    drawer_layout.addDrawerListener(drawerToggle)
    drawerToggle.setHomeAsUpIndicator(R.drawable.ic_arrow_back_white)
    drawerToggle.syncState()
    drawerToggle.setToolbarNavigationClickListener {
        // whatever action is needed on homeAsUp click
        onBackPressed()
}

//And this method should be called from fragment's onCreateView
fun showUpNavigation() {
    drawerToggle.isDrawerIndicatorEnabled=supportFragmentManager.getBackStackEntryCount() <= 1
}