setNavigationItemSelectedListener catches/blockes every menu item even if I return false

20 Views Asked by At

I fairly recently implemented a logout drawer menu for a better UX for my app. Here's the related code for it:

//drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:id="@+id/nav_grp_main" android:checkableBehavior="single">
        <item
            android:id="@id/nav_monitoring_fragment"
            android:title="@string/drawer_menu_monitoring" />
        <item
            android:id="@id/nav_dashboard_fragment"
            android:title="@string/drawer_menu_dashboard" />
    </group>
    <group android:id="@+id/nav_grp_logout" android:checkableBehavior="single">
        <item
            android:id="@+id/nav_logout"
            android:title="@string/logout" />
    </group>
</menu>

I won't add the nav_graph as it's quite long and isn't needed that much I feel for this question. Just remember that the ids nav_monitoring_fragment and nav_dashboard_fragment are present in the nav_graph.

//MainActivity.kt

appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.nav_monitoring_fragment, R.id.nav_dashboard_fragment, R.id.nav_logout, R.id.nav_login_fragment
        ), binding.drawerLayout
    )

    val navController = findNavController(R.id.nav_host_fragment_content_main)
    setupActionBarWithNavController(navController, appBarConfiguration)
    binding.navView.setupWithNavController(navController)

    navController.setOnBackPressedDispatcher(OnBackPressedDispatcher {
        binding.drawerLayout.closeDrawers()
    })

//  binding.navView.setNavigationItemSelectedListener{
//      when(it.itemId){
//          R.id.nav_logout -> {
//              logoutUser()
//               binding.drawerLayout.closeDrawers()
//          }
//      }
//
//      true
//  }

Because the id nav_logout doesn't exist in the nav_graph, I used setNavigationItemSelectedListener to give that the stuff it needs to do. If I remember correctly, everything worked correctly before. The normal drawer menu items worked correctly , it navigates me to the fragment it's "bound" to, and the logout does what logouts do. But after having some weird issue with the stuff related to navigation, I now got this issue. As you can see on the MainActivity code, I have commented out the code block of setNavigationItemSelectedListener. For some reason, if I have those commented out, the normal drawer menu items works again, but the logout stops working. If I return false for the other ids inside setNavigationItemSelectedListener, the normal drawer menu items still won't work but logout works. So what might be the fix for this issue? There's really some weird stuff going on on my app right now related to navigation, I feel.

0

There are 0 best solutions below