Prevent navigating to the same fragment

Viewed 5857

I'm using Android navigation jetpack library with BottomNavigationView. I have implemented the NavHost, the NavGraph and my fragments. Everything is working as intented when I use actions to navigate.

I use the following code to setup everything:

 val navController = Navigation.findNavController(this, R.id.nav_host)
 bottom_navigation.setupWithNavController(navController)

The problem is that if I click a tab 2 times the fragment is recreated twice. Is there any way to intercept navigation? I don't want to navigate to the same fragment that's being shown.

4 Answers

As per this issue,

Feel free to set a OnNavigationItemReselectedListener, which takes precedence over the OnNavigationItemSelectedListener set by NavigationUI.

val navController = Navigation.findNavController(this, R.id.nav_host)
bottom_navigation.setupWithNavController(navController)
bottom_navigation.setOnNavigationItemReselectedListener {
  // Do nothing to ignore the reselection
}

inside setOnItemSelectedListener use :

if( item.getItemId() == navController.getCurrentDestination().getId()){  return true; }

because OnNavigationItemSelectedListener is deprecated now.

I write this extension. It will check current fragment with destinations and if both are same, it will only close drawer. But there is a some animation problem while closing drawer.

fun NavigationView.setupWithUniqueFragment(navController: NavController) {

    this.setNavigationItemSelectedListener(object : NavigationView.OnNavigationItemSelectedListener {
        override fun onNavigationItemSelected(item: MenuItem): Boolean {
            val parent = this@setupWithUniqueFragment.parent
            if (item.itemId == navController.currentDestination?.id) {
                if (parent is DrawerLayout) {
                    parent.closeDrawer(this@setupWithUniqueFragment, true)
                }
                return true
            }
            val handled = NavigationUI.onNavDestinationSelected(item, navController)
            if (handled) {
                if (parent is DrawerLayout) {
                    parent.closeDrawer(this@setupWithUniqueFragment, true)
                }
            }
            return handled
        }

    })

    val weakReference = WeakReference<NavigationView>(this@setupWithUniqueFragment)
    navController.addOnDestinationChangedListener(
        object : NavController.OnDestinationChangedListener {
            override fun onDestinationChanged(
                controller: NavController,
                destination: NavDestination, arguments: Bundle?
            ) {
                val view = weakReference.get()
                if (view == null) {
                    navController.removeOnDestinationChangedListener(this)
                    return
                }
                val menu = view.menu
                var h = 0
                val size = menu.size()
                while (h < size) {
                    val item = menu.getItem(h)
                    item.isChecked = matchDestination(destination, item.itemId)
                    h++
                }
            }
        })
}

internal fun matchDestination(
    destination: NavDestination,
    @IdRes destId: Int
): Boolean {
    var currentDestination: NavDestination? = destination
    while (currentDestination!!.id != destId && currentDestination.parent != null) {
        currentDestination = currentDestination.parent
    }
    return currentDestination.id == destId
}

I had this same problem where I am using an About page (using the really good AboutLibraries), and it would stack up duplicate pages. I ended up doing this in my OnOptionsItemSelected method

            case R.id.action_about:
            NavController navController = Navigation.findNavController( this, R.id.nav_host_fragment );
            if ( navController.getCurrentDestination().getId() != R.id.nav_about )
            {
                navController.navigate( R.id.nav_about );
            }

And now it only shows a single fragment and won't stack the same one ontop. It's a shame this behaviour couldn't be defined in the XML and I needed to do this in code.

Related