My application contains a bottom navigation view with 3 menu items in the main activity, each menu item inflates respective navigation graphs in the navigation container view. Each graph has 2 or more fragments connected via actions.
The problem here is during screen orientation change the application gets crashed. Also, bottom navigation does not preserve navigation graphs's state and there is no backstack maintained for the bottom navigation.
Code sample below.
val bottomNavigation = binding.bottom_navigation_view
navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController
bottomNavigation.setOnNavigationItemSelectedListener{ item ->
when(item.itemId){
R.id.navigation_home ->{
val navGraph = navController.navInflater.inflate(R.navigation.nav_graph)
navController.graph = navGraph
true
}
R.id.navigation_search ->{
val searchGraph=navController.navInflater.inflate(R.navigation.search_nav_graph)
navController.graph = searchGraph
true
}
R.id.navigation_about ->{
val infoGraph = navController.navInflater.inflate(R.navigation.info_nav_graph)
navController.graph = infoGraph
true
}
}
false
}