4

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.

  1. i have a nav graph with 4 fragments

  2. 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)
                }
    
    
  3. in manifest for this activity i added <nav-graph android:value="@navigation/nav_unauth_state" />

  4. in nav_unauth_state for frgament i set the deeplink

            android:id="@+id/deepLink2"
            app:uri="https://<my_secret_url>/{action}" />
Alex Alex
  • 131
  • 7

1 Answers1

3

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
}
Alex Alex
  • 131
  • 7
  • 2
    The problem was in setRestoreState(true) , set to setRestoreState(false and works – Alex Alex Jun 17 '21 at 09:42
  • 2
    Thanks for this answer it helped me. But it also removes the saved back stack functionality. I hope someone is able to find a solution that keeps the saved back stack – fluxeon Aug 24 '21 at 21:26