Ladislav Rolnik
Ladislav Rolnik

Reputation: 176

BottomNavigationView OnNavigationItemSelectedListener doesn't triggered

I have this code in my BottomActivity.

 private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                Log.d("Navigation", "You are picking bottombar")
                supportActionBar?.title = "Navigation Feed"
                val dashboardFragment = DashboardFragment()
                openFragment(dashboardFragment)
                return@OnNavigationItemSelectedListener true
            }

            R.id.navigation_dashboard -> {
                val homeFragment = HomeFragment()
                openFragment(homeFragment)
                return@OnNavigationItemSelectedListener true
            }

            R.id.navigation_notifications -> {
                val notificationFragment = NotificationsFragment()
                openFragment(notificationFragment)
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bottom)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)


        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

        val navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

    private fun openFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.container, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

All what it should do is setting listener and opening my fragments but it doesn't. I think that problem is somewhere in mOnNavigationItemSelectedListener because it even doesnt' log on console. My activity is loading just first fragment with text This is home fragment and bottomview doesn't react on click. I need solution in kotlin language.

Upvotes: 0

Views: 1015

Answers (1)

metis
metis

Reputation: 1044

For navigation component to work correctly, you do not need BottomNavigationView.OnItemSelectedListener actually. In OnItemSelectedListener, loading fragments manually is exacly WRONG behaviour. If you really need it, you should do it in the following way:

 bottomNav.setOnNavigationItemSelectedListener {
        // ...
        NavigationUI.onNavDestinationSelected(it, navController)
        // ...
    }

Check the ids of your menu items in menu_bottomnavigationview.xml. Check the ids of your top level fragments in your navigation graphs, namely R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications. These ids should be the same. Thats enough for navigation controller to load correct fragment into the NavHostFragment.

In addition to this, if you write BottomNavigationView.OnItemSelectedListener, then you overrided the default behaviour. Then navigation controller cannot load correct fragment into the NavHostFragment. If you really need to use BottomNavigationView.OnItemSelectedListener, then you should write the following code for loading fragments into NavHostFragment.

bottomNav.setOnNavigationItemSelectedListener {
        NavigationUI.onNavDestinationSelected(it, navController)
    }

Upvotes: 2

Related Questions