Let's say we have a bottom navigation view with 4 tabs, we have a deeplink for a fragment of 4 tab, so when navigating from this deeplink to 4 tab - it works as it should, but when after that manually select the first tab, a fragment of 4 tab is added also in the first tab.
i have a nav graph with 4 fragments
in on create of my activity i set the bottom view with nav controller
navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as? NavHostFragment
navController = navHostFragment?.navController
navController?.setGraph(R.navigation.nav_unauth_state)
navView.inflateMenu(getBottomNavViewMenu())
navController?.let {
navView.setupWithNavController(it)
}
in manifest for this activity i added <nav-graph android:value="@navigation/nav_unauth_state" />
in nav_unauth_state for frgament i set the deeplink
android:id="@+id/deepLink2"
app:uri="https://<my_secret_url>/{action}" />
i solved it my self with this solution
navView.setOnItemSelectedListener { menuItem ->
val builder = NavOptions.Builder().setLaunchSingleTop(true).setRestoreState(false)
val graph = navController?.currentDestination?.parent
val destination = graph?.findNode(menuItem.itemId)
if (menuItem.order and Menu.CATEGORY_SECONDARY == 0) {
navController?.graph?.findStartDestination()?.id?.let {
builder.setPopUpTo(
it,
inclusive = false,
saveState = true
)
}
}
val options = builder.build()
destination?.id?.let { id -> navController.navigate(id, null, options) }
return@setOnItemSelectedListener true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With