Restore menu items states from custom BottomNavigationView using Jetpack Navigation
19:30 15 Aug 2021

How can I setupWithNavController a custom BottomNavigationView with an inset FloatingActionButton (FAB)?

Inset FAB

According to Jetpack Navigation…

Starting in Navigation 2.4.0-alpha01, the state of each menu item is saved and restored when you use setupWithNavController.

This works like a charm, however, when I setup my BottomNavigationView and press the FAB, the states of the current menu items are reloaded. How can I achieve the same behavior of clicking on any menu item with the FAB?

binding.activityMainBottomNavigationView.setupWithNavController(navController)

binding.activityMainFloatingActionButton.setOnClickListener {

    // TODO: Fix navigation.
    navController.navigate(R.id.homeFragment)

    binding.activityMainBottomNavigationView.selectedItemId = R.id.homeFragment      
}

As I understand, calling navController.navigate() may be resetting the current nav_graph used by the fragmentContainerView and that's why the current menu items are being reloaded. Is there any way to avoid this?

android navigation floating-action-button bottomnavigationview