Skip to content

Instantly share code, notes, and snippets.

@deckyfx
Last active March 13, 2020 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deckyfx/fbf2d2dd6581f5c821c94045b3173b0a to your computer and use it in GitHub Desktop.
Save deckyfx/fbf2d2dd6581f5c821c94045b3173b0a to your computer and use it in GitHub Desktop.
Clear BottomNavigation item stack before navigate away
// first i extends BottomNavigationView class so it can attach multiple "setOnNavigationItemSelectedListener"
class ExtendedBottomNavigationView(
context : Context,
attrs : AttributeSet?= null,
defStyleAttr: Int = 0
) : BottomNavigationView(context, attrs, defStyleAttr) {
constructor (context : Context) : this(context, null, 0)
constructor (context : Context, attrs : AttributeSet) : this(context, attrs, 0)
private val selectedListeners = mutableListOf<(menuItem : MenuItem) -> Boolean>()
fun addOnNavigationItemSelectedListener(listener: (menuItem : MenuItem) -> Boolean) {
this.selectedListeners.add(listener)
this.setOnNavigationItemSelectedListener {
var result = false
for (i in 0 until this.selectedListeners.size) {
result = this.selectedListeners[i].invoke(it)
if (result) {
continue
} else {
break
}
}
result
}
}
}
// modify google samples NavigationExtension.kt extension function to use our class
fun ExtendedBottomNavigationView.setupWithNavController(
navGraphIds : List<Int>,
fragmentManager : FragmentManager,
containerId : Int,
intent : Intent
): LiveData<NavController> {
...
// replace "setOnNavigationItemSelectedListener" to use this
addOnNavigationItemSelectedListener { item ->
...
}
...
}
// in your activity
private fun setupBottomNavigationBar() {
val navGraphIds = listOf(
...,
...,
...,
...,
...)
// attach listener to your bottom tab nav view
this.extended_bottom_navigation_view?.addOnNavigationItemSelectedListener {
// must return boolean, true to proceed tab changes, or false to halt it
// depends on your app behaviour, you may want to exclude some tabs
when (it.itemId) {
... -> {
true
}
else -> {
this.clearBackStackBeforeMoveTab(it)
}
}
}
}
private fun clearBackStackBeforeMoveTab(it: MenuItem): Boolean {
// must return boolean, true to proceed tab changes, or false to halt it
// try various methods to clean backstack, or set the startDestination / destination to root
return if (willBeAbleToBackStack == null) {
try {
this.currentNavController?.value?.navigate(it.itemId, null,
NavOptions.Builder().setPopUpTo(it.itemId, true).build())
true
} catch (e: Exception) {
e.printStackTrace()
true
}
} else {
if (it.itemId == this.currentNavController?.value?.graph?.id) {
try {
this.currentNavController?.value?.navigate(this.currentNavController?.value?.graph?.startDestination?: 0, null,
NavOptions.Builder().setPopUpTo(it.itemId, true).build())
val newPosition = this.activity_post_auth__bottom_navigation_view?.postionForItem(it)
if (newPosition != null) this.activity_post_auth__bottom_navigation_view?.checkItem(newPosition)
true
} catch (e: Exception) {
e.printStackTrace()
true
}
} else {
this.currentNavController?.value?.navigateUp() != true
true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment