Skip to content

Commit

Permalink
Fix onBackPressed for false value
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNatan committed Dec 10, 2023
1 parent cbe49cd commit 372d828
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ public class Navigator @InternalVoyagerApi constructor(
}
}

internal fun popRecursively() {
if (pop()) return
parent?.popRecursively()
}

@InternalVoyagerApi
public fun dispose(
screen: Screen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ internal fun NavigatorBackHandler(
navigator: Navigator,
onBackPressed: OnBackPressed
) {
if (onBackPressed != null) {
if (onBackPressed == null) {
BackHandler(
enabled = navigator.canPop || navigator.parent?.canPop ?: false,
enabled = navigator.size > 1,
onBack = navigator::popRecursively
)
} else {
// `navigator.size == 1` covers onBackPressed = { false } and empty stack
// because `navigator.canPop` always returns `true` when stack min size is 1
BackHandler(
enabled = (navigator.size == 1 && !onBackPressed(navigator.lastItem)) ||
navigator.canPop ||
navigator.parent?.canPop ?: false,
onBack = {
if (onBackPressed(navigator.lastItem)) {
if (navigator.pop().not()) {
navigator.parent?.pop()
}
}
if (onBackPressed(navigator.lastItem))
navigator.popRecursively()
}
)
}
Expand Down

0 comments on commit 372d828

Please sign in to comment.