Hi I try to create the bottom navigation bar for Android device using Kotlin. I just learn the Android development, and faced with strange behaviors. My navigation works but for open next fragment I need to double click on the menu item. I think the issue in this part of 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(R.id.bottom_navigation) line. I think so but I can not solve the issue. So I will many thanks for helping and explanation.
CodePudding user response:
Please use setOnItemSelectedListener
instead of setOnItemReselectedListener
.