I am fairly new to Android programming. I have looked for answers everywhere but I don't think I know the source of this issue. Here's the problem :
I added a Navigation Drawer to my app today, and I have 5 different fragments which I wanna switch between each other. Everything is going smoothly except for the most important thing: Calling something when the navigation drawer buttons are clicked.
For the sake of simplicity, the Navigation Drawer for now only has TWO buttons: Home and About.
Here's my onNavigationItemSelected :
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.nav_home -> {
Toast.makeText(this, "Home clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_about -> {
Toast.makeText(this, "About clicked", Toast.LENGTH_SHORT).show()
}
}
binding.drawerLayout.closeDrawer(GravityCompat.START)
return true
}
As you can see, the drawer DOES get closed, but no Toast appears. I already added this below OnCreate:
navigationView.setNavigationItemSelectedListener(this)
So I don't know what's exactly happening?! Why is onNavigationItemSelected executing but not the "when" expression?!
There is no error, no warning, no crash, not a single thing. I click on the buttons and nothing happens at all.
It was right in front of my eyes the whole time, these two don't function well together:
navigationView.setupWithNavController(navController)
navigationView.setNavigationItemSelectedListener(this)
I removed the first line and kept the second, now all transactions of fragments are happening and the onNavigationItemSelected is working flawlessly.