In my Android project I have a very simple Navigation Graph, including two fragments: Master and Detail:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/wordsListFragment">
<fragment
android:id="@+id/wordsListFragment"
android:name="com.***.presentation.view.WordsListFragment"
android:label="List"
tools:layout="@layout/words_list_fragment">
<action
android:id="@+id/action_wordsListFragment_to_wordDetailsFragment"
app:destination="@id/wordDetailsFragment" />
</fragment>
<fragment
android:id="@+id/wordDetailsFragment"
android:name="com.***.presentation.view.WordDetailsFragment"
android:label="Details"
tools:layout="@layout/word_details_fragment" />
</navigation>
The navigation itself works perfectly fine in both directions including the "Back" behaviour.
In that project I have a single activity where I implement OnDestinationChangedListener
.
All this according to the following documentation from Google: NavController Updating UI
I call the following method when the user clicks on a list item (while being on the master fragment):
findNavController().navigate(R.id.action_wordsListFragment_to_wordDetailsFragment, null)
Then in the parent activity I have the following implementation:
private fun setupNavController() {
navigationController = findNavController(R.id.nav_mainhost_fragment_container)
navigationController.addOnDestinationChangedListener(mainDestinationChangedListener)
appBarConfiguration = AppBarConfiguration(navigationController.graph)
setupActionBarWithNavController(navigationController, appBarConfiguration)
}
and that is the listener object:
private val mainDestinationChangedListener =
NavController.OnDestinationChangedListener { controller, destination, arguments ->
if (destination.id == R.id.action_wordsListFragment_to_wordDetailsFragment) {
actionBar?.hide()
} else {
actionBar?.show()
}
}
but the destination.id
does not match the R.id.action_wordsListFragment_to_wordDetailsFragment
I have tried to clean up the project, clean up the IDE cache, the gradle cache, but the generated identifiers still does not match. I have also tried to use Navigation via Safe Args:
val action = WordsListFragmentDirections.actionWordsListFragmentToWordDetailsFragment()
findNavController().navigate(action)
but the results in the given listener are always the same (i.e. not matching).
Some values from debugging:
findNavController().navigate(1000021) //R.id.action_wordsListFragment_to_wordDetailsFragment
but the next call on stack has another value:
what also matches the
destination.id
values passed to the OnDestinationChangedListener
:
destination.id //2131231018
Any hints from your side are more than welcome. I just want to recognise the destination or the action ID and adjust the ToolBar accordingly.