Navigationで画面遷移を実装している場合、こんなふうに実現できます。
Fragment側からActivityのリソースにアクセスする必要がないので、Fragmentの独立性が維持できていいですね。
MainActivity.kt
private fun setupBottomNavigationBar() {
val navGraphIds =
listOf(R.navigation.tab_1, R.navigation.tab_2, R.navigation.tab_3)
val controller = bottomNavigation.setupWithNavController(
navGraphIds = navGraphIds,
fragmentManager = supportFragmentManager,
containerId = R.id.nav_host_container,
intent = intent
)
controller.observe(this, Observer { navController ->
if (navController.graph.id == R.id.tab_1) {
navController.addOnDestinationChangedListener { controller, destination, arguments ->
val s = resources.getResourceName(destination.id)
info { "resourceName:$s" }
// tab1でFullScreenFragmentが表示される時に、AppBarLayoutとBottomNavigationViewを非表示
if (destination.id == R.id.fullScreenFragment) {
bottomNavigation.visibility = View.GONE
toolbar.visibility = View.GONE
appBarLayout.setExpanded(false, true)
} else {
bottomNavigation.visibility = View.VISIBLE
toolbar.visibility = View.VISIBLE
appBarLayout.setExpanded(true, true)
}
}
setupActionBarWithNavController(navController)
}
})
currentNavController = controller
}