Why for open fragment I need double click to menu item?

Viewed 116

I am trying to create the bottom navigation bar for Android devices using Kotlin. I learned Android development and was faced with strange behaviors. My navigation works but to open the next fragment I need to double click on the menu item. I think the issue in this part of the code:

private val settingsFragment = SettingsFragment()
private val informationFragment = InformationFragment()
private val supportFragment = SupportFragment()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val bottom_navigation = findViewById<BottomNavigationView>(R.id.bottom_navigation)
    replaceFragment(informationFragment)
    bottom_navigation.setOnItemReselectedListener {
        when(it.itemId){
            R.id.app_info -> replaceFragment(informationFragment)
            R.id.app_settings -> replaceFragment(settingsFragment)
            R.id.app_support -> replaceFragment(supportFragment)
        }
    }
}

private fun replaceFragment(fragment: Fragment){
    if(fragment != null){
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.fragment_container, fragment)
        transaction.commit()
    }
}

From val bottom_navigation = findViewById<BottomNavigationView>(R.id.bottom_navigation) line. I think so, but I can not solve the issue. So I will say many thanks for helping and explaining.

1 Answers

Please use setOnItemSelectedListener instead of setOnItemReselectedListener.

Related