Home > Enterprise >  Switch between Hamburger icon and Back arrow icon in navigation drawer in spcific fragment not worki
Switch between Hamburger icon and Back arrow icon in navigation drawer in spcific fragment not worki

Time:10-27

I am trying to change the navigation icon when opening the settings fragment using enter image description here

CodePudding user response:

Try this

navController.addOnDestinationChangedListener { _, destination, _ ->
            Log.d(TAG, "addOnDestinationChangedListener: "   destination.id)

            if (destination.id == R.id.settingsFragment) {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                    drawerLayout.closeDrawer(GravityCompat.START)

                }

               binding.toolbar.setNavigationOnClickListener { //do whatever you want here

                   navController.navigateUp()
               }


            } else {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)

                binding.toolbar.setNavigationOnClickListener{
                    drawerLayout.openDrawer(GravityCompat.START)
                }

            }


   override fun showBackIcon() {
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowHomeEnabled(true)
        binding.toolbar.setNavigationIcon(R.drawable.ic_arrow_back_24)
    }

and remove showHamburgerIcon & ActionBarDrawerToggle codes

  • Related