1

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
kxko
  • 154
  • 1
  • 10
  • You see the default button when the new Activity is showing? Maybe you are replacing it in the wrong place. Activity is being created, view is created, then half second after you replace the icon. Consider setting the activity view after you have changed that icon in the activity that you are going. If you prefer, post full code and I could help you more. – Bogdan Android Oct 14 '20 at 14:25
  • @BogdanAndroid i'm using fragments and JetPack navigation. – kxko Oct 14 '20 at 14:26
  • So, you are replacing the icon on the activity? or do you have a toolbar in each fragment? – Bogdan Android Oct 14 '20 at 14:28
  • @BogdanAndroid i have a toolbar in my activity (initialization and setting nav controller logic for it). In base fragment i have a logic to setting and handling this icon (depending on where i am now i'm setting to this icon to null (i don't want to see hamburger icon when i'm in parent)). Maybe this is a not appropriate place to having such code or it is ok ? – kxko Oct 14 '20 at 14:32
  • Are you setting your navigationIcon in the navController.addOnDestinationChangedListener ? – Gabriele Mariotti Oct 14 '20 at 14:32
  • @GabrieleMariotti no – kxko Oct 14 '20 at 14:34

1 Answers1

1

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 {
       //
   }
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • What is the difference between setting navigation icon in this place and the place, where i'm setting ? (onViewCreated) – kxko Oct 14 '20 at 15:02
  • @kxko The difference is that you are using the Navigation components which works under your code and sets the icon for you. – Gabriele Mariotti Oct 14 '20 at 15:08
  • Hm, thanks, i will try it and will let you know ! Could you please give me link that information ? Because i didn't find it at android.developer (or maybe i'm blind :) ) Thanks a lot ! – kxko Oct 14 '20 at 15:11
  • You can check https://developer.android.com/guide/navigation/navigation-ui#appbarconfiguration. In particular *"When the user is on any other destination, the Navigation button appears as an Up button"* – Gabriele Mariotti Oct 14 '20 at 15:18
  • Why do we have to change it all the time fragment is changed? Can't we just override Menu, Back button somehow? https://stackoverflow.com/questions/69812362/how-to-set-my-own-menu-and-back-icons-for-toolbar Because in your case you would need to know which fragment is root or for navigation bottom or non-root. Navigation Component handles this logic by itself – user924 Nov 02 '21 at 15:10
  • What is xxx can you please explain? – Md Imran Choudhury Oct 04 '22 at 10:19
  • @MdImranChoudhury The resource ID of destination. – Gabriele Mariotti Oct 04 '22 at 10:30