1
votes

Help me out please.
Intro: I use Navigation Component, Single Activity pattern with 3 fragments switched via bottom navigation menu. Also I have Drawer Navigation.
All fragments are of the same level (all are root, accessed directly from bottm nav)
How things should be: For all fragments there must be a toolbar with hamburger icon for the drawer.
Problem: when app starts, the home fragment shows standard hamburger icon for drawer, which is ok. But when I switch to any other fragment, the drawer icon turns into arrow icon. Furthermore, when ths arrow is pressed, the drawer slides from left. It means it still works as a button for showing the Drawer menu, but only icon changed.
Question: How can I disable transformation of hamburger icon into arrow icon when switching to another fragments from bottom navigation menu?
Files: Navigation Graph:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/bottom_nav_proposals"
    >
    <fragment
        android:id="@id/bottom_nav_proposals"
        android:name="com.base.ProposalsContainerFragment"
        android:label="fragment_proposals"
        tools:layout="@layout/fragment_proposals" />
    <fragment
        android:id="@id/bottom_nav_vehicles"
        android:name="com.base..DriversVehiclesFragment"
        android:label="Vehicles" >
    </fragment>
    <fragment
        android:id="@id/bottom_nav_drivers"
        android:name="com.bijov1apps.base.carrier.root.drivers.DriversVehiclesFragment"
        android:label="Drivers" >
    </fragment>
</navigation>

Activity:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//initializing Navigation COmponent
       val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_carrier_root) as NavHostFragment
        val navController = navHostFragment.navController
//setting up toobar stuff
        val toolbar: Toolbar = findViewById(R.id.toolbar_root)
        toolbar.setupWithNavController(navController, drawerLayout)
//setting up navigation drawer stuff
        val drawerLayout:DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val toggle = ActionBarDrawerToggle(
            this, drawerLayout, toolbar, R.string.navigation_drawer_open, 
        R.string.navigation_drawer_close
        )
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()
        navView.setNavigationItemSelectedListener(this)
//setting up bottom navigation menu stuff
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.logisticBottomBar)
        bottomNavigationView.setupWithNavController(navController)
}
1
Make sure your IDs in the Navigation Graph and in the menu XML file are matching. Refer to this Video for more information: youtu.be/wv5VFEcnb-8?t=217 (The video will start at the correct timestamp when he explains the IDs).René Jörg Spies
According to developer.android.com/guide/navigation/… the newest version of the navigation drawer overlaps the top bar and the hamburger icon or back arrow shouldn't even be visible.ich5003
@RenéSpies, everything is correct, all IDs are matchingWaldmann
Listen closely to what the man in the video says. You are using the @+id/ which is wrong in your case.René Jörg Spies
@ich5003 The question is about icons in toolbar when the drawer menu is hidden.Waldmann

1 Answers

3
votes

As per the Navigation Top App Bar documentation:

NavigationUI uses an AppBarConfiguration object to manage the behavior of the Navigation button in the upper-left corner of your app's display area. The Navigation button’s behavior changes depending on whether the user is at a top-level destination.

A top-level destination is the root, or highest level destination, in a set of hierarchically-related destinations. Top-level destinations do not display an Up button in the top app bar because there is no higher level destination. By default, the start destination of your app is the only top-level destination.

When the user is at a top-level destination, the Navigation button becomes a drawer icon if the destination uses a DrawerLayout. If the destination doesn't use a DrawerLayout, the Navigation button is hidden. When the user is on any other destination, the Navigation button appears as an Up button.

Therefore if you want the drawer icon to appear on all of your top level icons, you need to create an AppBarConfiguration that lists those destinations and use that when you call setupWithNavController():

val appBarConfiguration = AppBarConfiguration(
    setOf(R.id.bottom_nav_proposals, R.id.bottom_nav_vehicles, R.id.bottom_nav_drivers), drawerLayout)
toolbar.setupWithNavController(navController, appBarConfiguration)

The Navigation Drawer documentation on that same page also specifically point out that you should not be using ActionBarDrawerToggle at all when using Navigation. You must remove all of that code.