Search code examples
androidnavigation-drawerandroid-navigation

How to configure the Drawer Navigation button when not using the start as the top-level navigation?


I'm using the navigation graph to navigate and have a start screen (splash fragment) as the first fragment that is shown. The issue is that when I go to the main fragment/screen the top navigation button shows back instead of the drawer icon. How can we get control over this? What are some options. How can I change the start destination? (if possible)

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 . To configure the Navigation button using only the start destination as the top-level destination, create an AppBarConfiguration object, and pass in the corresponding navigation graph, as shown below:

Example Code

My issue is that the back button is still present when navigating to next_fragment. It should show the menu/hamburger icon.

class MainActivity : AppCompatActivity() {

private lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration : AppBarConfiguration

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)

    val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
    drawerLayout = binding.drawerLayout

    val navController = this.findNavController(R.id.mainNavigationHostFragment)
    NavigationUI.setupActionBarWithNavController(this, navController)

    appBarConfiguration = AppBarConfiguration(setOf(R.id.nextFragment), drawerLayout) 
    NavigationUI.setupWithNavController(binding.mainNavigationDrawerView, navController)

    val navigationHeader = binding.mainNavigationDrawerView.getHeaderView(0)
    val iconButton = navigationHeader.findViewById<ImageButton>(R.id.main_nav_icon_button)
    

}

override fun onSupportNavigateUp(): Boolean {

    val navController = this.findNavController(R.id.mainNavigationHostFragment)
    return NavigationUI.navigateUp(navController, appBarConfiguration)
}

}


Solution

  • Instead of passing Navigation Graph to AppbarConfiguration, pass the id of the Fragment that should show the HamBurger icon, that way when the particular Fragment is shown, it shows home/hamburger icon instead of back icon.

    That is

    val appBarConfiguration = AppBarConfiguration(setOf(R.id.FAGMENT_THAT_SHOULD_SHOW_HOME_ICON), DRAWER_LAYOUT)

    Update

    Instead of

    val navController = this.findNavController(R.id.mainNavigationHostFragment)
    
    NavigationUI.setupActionBarWithNavController(this, navController) 
    
     appBarConfiguration = AppBarConfiguration(setOf(R.id.nextFragment), drawerLayout) 
        NavigationUI.setupWithNavController(binding.mainNavigationDrawerView, navController)
    
    val navigationHeader = binding.mainNavigationDrawerView.getHeaderView(0)
      val iconButton = navigationHeader.findViewById<ImageButton>(R.id.main_nav_icon_button)
        
    

    Just

    val navController = this.findNavController(R.id.mainNavigationHostFragment)
    
    appBarConfiguration = AppBarConfiguration(setOf(R.id.nextFragment), drawerLayout) 
        
    
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);