I have an app with single activity and bottom navigation view..
There are 4 items in bottom navigation so i have 4 fragments for it..
My question is how can I handle back stack with bottom navigation view like Youtube or Instagram?
I'm using Kotlin and this is my code:
nav_view.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.navigation_home -> {
replaceFragment(homeFragment)
true
}
R.id.navigation_projects -> {
replaceFragment(projectsFragment)
true
}
R.id.navigation_team -> {
replaceFragment(teamFragment)
true
}
R.id.navigation_contact -> {
replaceFragment(contactUsFragment)
true
}
else -> false
}
}
private fun replaceFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit()
}
override fun onBackPressed() {
if (nav_view.selectedItemId == R.id.navigation_home) {
super.onBackPressed()
} else {
nav_view.selectedItemId = R.id.navigation_home
}
}