I Have an app with a nav drawer and say 4 destinations in my nav drawer:
- Home
- FragmentAWithViewPager2 > FragmentA1 > FragmentA2 > FragmentA3
- FragmentBWithViewPager2 > FragmentB1 > FragmentB2 > FragmentB3
- FragmentCWithViewPager2 > FragmentC1 > FragmentC2 > FragmentC3
The Nav Drawer Initially Works Fine from all fragments to all destinations, but when I navigate from any of the child fragments i.e FragmentB2 back to the Home Fragment via a button click;
override fun onClick(v: View) { if (v.id == R.id.connect) { findNavController().navigate(R.id.fragmentWithViewpager2_to_homeFragment) }
This navigation works fine taking me back to the Home fragment.
I then open my Nav Drawer and click on FragmentBWithViewPager2 and nothing happens ???
To try to find out what is going on I have added the destination change listener
navController.addOnDestinationChangedListener { navController, destination, bundle ->
Timber.d("controller onDestinationChanged label: ${destination.label} ")
}
So I open my nav drawer press to navigate to FragmentBWithViewPager2 and logcat gives
"controller onDestinationChanged label: Home"
I've removed this listener and added a NavigationItemSelectedListener;
bindings.navView.setNavigationItemSelectedListener {
Timber.d("controller setNavigationItemSelectedListener ${it.itemId}")
navController.navigate(it.itemId)
bindings.drawerLayout.closeDrawer(GravityCompat.START);
return@setNavigationItemSelectedListener true
}
And the Navigation Works Again....????