Search code examples
androidnavigationandroiddesignsupport

Back button not displaying using navigation view and toolbar


I have an application with the following hierarchy:

MainActivity (Shows list of dates) 
    | 
ViewPagerFragment (Shows list of children for those dates)
    | 
ChildFragment (Detail View)

I am trying to implement the navigation view from the design support library, but am having trouble getting the actual navigation on the toolbar to work.

Here is the Main Activity toolbar:

main activity navigation

Here is the ViewPagerFragment after navigating there from the main activity, note that there is no back button...

child activity navigation

Here is the desired toolbar: desired toolbar

I am adding fragments using the following code:

    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame, fragment, fragment.getClass().getName())
            .addToBackStack(fragment.getClass().getName())
            .commitAllowingStateLoss();

Here is the code related to activity startup:

protected void setupActionBar() {
    setSupportActionBar(toolbar);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

And my navigation drawer setup:

protected void setupNavigationDrawer() {
    navigationView.setNavigationItemSelectedListener(this);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
}

Hitting the hardware back button on the device navigates back properly. I just can't seem to get the back arrow to show up on the drawer toggle... Any suggestions?


Solution

  • To disable the navigation drawer icon and show a different icon instead, you need to call setDrawerIndicatorEnabled(false) on your ActionBarDrawerToggle.

    You may also need to call setHomeAsUpIndicator() to specify the icon you want to use instead of the "hamburger" icon.