Search code examples
androidtoolbarandroid-toolbarandroid-architecture-navigationandroid-jetpack-navigation

Toolbar back button replaced for default icon for a half second when clicking on it


i'm trying to replace default back button icon on toolbar like this:

toolbar.navigationIcon = R.drawable.lalala

it's fine, and it's working. But, when i'm trying to click back button, for a half second i can see default icon instead of mine. What i can do wrong ?

I'm using fragments and JetPack navigation.


Solution

  • Since you are using the Navigation Components, the expected behavior is the Up button displayed when you are on a non-root destination.
    You can change it using the addOnDestinationChangedListener after your setup method.

    Something like:

    navController.addOnDestinationChangedListener { _, destination, _ ->
       if(destination.id == R.id.xxxx) {
           toolbar.setNavigationIcon(R.drawable.xxxx)
       } else {
           //
       }
    }