Search code examples
androidbottomnavigationview

Bottom Navigation in Android works only on double clicks ,


Creating an app ,and implementing the bottom navigation through setting up the "On navigation selected listener",On clicking the icons in the bottom nav panel , i am trying to replace fragments, The fragments do get replaced but on double clicks. A single click on the menu item does nothing. Here is my Code '''

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

            setContentView(R.layout.activity_main)
   // val toolbar=app_bar as androidx.appcompat.widget.Toolbar
    //setSupportActionBar(toolbar)

 val navigation = findViewById<View>(R.id.nav_view) as BottomNavigationView
    navigation.setOnNavigationItemReselectedListener {

        when (it.itemId) {
            R.id.navigation_home -> {

                var x = supportFragmentManager
                var y = x.beginTransaction()

                var fragment = HomeFragment()
                y.replace(R.id.nav_host_fragment, fragment)
                y.addToBackStack("123")
                y.commit()


            }

            R.id.navigation_login -> {

                var x = supportFragmentManager
                var y = x.beginTransaction()

                var fragment = LoginFragment()
                y.replace(R.id.nav_host_fragment, fragment)
                y.addToBackStack("456")
                y.commit()


            }

            R.id.navigation_notifactory -> {

                var x = supportFragmentManager
                var y = x.beginTransaction()

                var fragment = NotificationFragment()
                y.replace(R.id.nav_host_fragment, fragment)
                y.addToBackStack("789")
                y.commit()


            }




        }

    }


    var x = supportFragmentManager
    var y = x.beginTransaction()

    val fragment=HomeFragment()

    y.add(R.id.nav_host_fragment,fragment)
    y.addToBackStack(null)
    y.commit()
}



}

'''


Solution

  • replace this :

        navigation.setOnNavigationItemReselectedListener {
    

    by:

       navigation.setOnNavigationItemSelectedListener {