Navigation related components are behaving differently

49 Views Asked by At

After not touching the code of my android project for a while because it's already completed now, I noticed that the navigation menu is acting weird for some reason. If I click the hamburger menu, it just reloads the current Fragment, and the CoordinatorLayout didn't open. If I swipe right to open the CoordinatorLayout, it does open, but the normal item doesn't seems to work properly. Currently, I only have 2 "normal" items in the navigation menu, and a "custom" item for logout. If I click the not currently selected item on the navigation menu, nothing happens. But logout works fine. When I click it, the things I said it needs to do inside binding.navView.setNavigationItemSelectedListener gets executed. Also, some methods under navController is throwing a lint error that says:

NavController.setOnBackPressedDispatcher can only be called from within the same library group (referenced groupId=androidx.navigation from groupId=[My project name])

setOnBackPressedDispatcher and findNavController().currentBackStack has that lint error. Even if I got the weird lint error, the app still build, at least. I'm currently using version 2.74 of androidx.navigation:navigation-fragment-ktx and androidx.navigation:navigation-ui-ktx.

UPDATE:

When I'm in the login fragment, if I click the hamburger menu, the app crashes without any error showing in Logcat. And if I'm inside the dashboard Fragment, it takes 2 clicks on the hamburger menu before the app crashes, also no error message as well.

I'll add in my related code so you can check it out if you want. It's mostly the usual boiler plate for the navigation system.

private lateinit var appBarConfiguration: AppBarConfiguration

appBarConfiguration = AppBarConfiguration(
    setOf(
        R.id.nav_monitoring_fragment, R.id.nav_login_fragment, R.id.nav_dashboard_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
        }
        else -> false
    }
}

UPDATE:

I'm not sure if I actually found what is causing my issue. After commenting the code below seemed to have fixed the crashes or the misbehaviour of clicking the hamburger menu. Here's the code:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == android.R.id.home) {
        onBackPressed()
        return true
    }
    return true
}

To be honest, I already forgot why I have that, but I might actually not need it anymore so it's nice that it's the only reason for the issue. But still, it's kinda weird that that's the reason for the bug/crash. Doesn't menu and the drawer menu not linked to each other?

0

There are 0 best solutions below