Issue
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.
Solution
Please use setOnItemSelectedListener
instead of setOnItemReselectedListener
.
Answered By - Ghulam Rasool
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.