NavController.OnDestinationChangedListener passes a destination.id not matching the triggered Navigation Action ID
You are comparing fragmentId with actionId so it's always false
here if (destination.id == R.id.action_wordsListFragment_to_wordDetailsFragment)
As destination.id
is fragmentId and R.id.action_wordsListFragment_to_wordDetailsFragment
is actionId
To make it work you should compare two fragment ids like
if (destination.id == R.id.wordDetailsFragment)
*Edit
First you should find you navControler , then listen to its destination change.
val navController = findNavController(this,R.id.nav_host_fragment)// this maybe changenavController.addOnDestinationChangedListener { controller, destination, arguments -> if(destination.id == R.id.wordDetailsFragment) { actionBar?.hide() } else { actionBar?.show() }}
The issue is with the Android studio debugger. If I print the value in logcat or assign it to a variable, then the value is 2XXXXXXXXX but if I evaluate with debugger then the value is 1XXXXXX.
I have checked the action id(R.id.xxxx) hex code in the APK. When I convert the hex code into integer, it gives a value in 2XXXXXXXXX which is equal to the value that got printed in the logcat
The above solution does not work, Instead of matching on the destination fragment id, you can match on the fully qualified class name of the destination fragment, your code becomes
navController.addOnDestinationChangedListener{ nc: NavController, nd: NavDestination, args: Bundle? -> val destinationClassName = (nc.currentDestination as FragmentNavigator.Destination).className when(destinationClassName) { "com.*domainName.*PackageName.*className" -> { "Do you thing here" } else -> { "Cater for the else block here" } } }