android / android/nowinandroid

[Bug]: Unintended back navigation occurs when rapidly tapping the back button in `topicScreen`

Open
#1,571 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Kotlin
Stars
21.8k
Forks
4.6k
Avg merge
19h 20m
Merged PRs (30d)
2

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is there a StackOverflow question about this issue?

- [X] I have searched StackOverflow

### What happened?

When rapidly tapping the back button in the `topicScreen` component, unintended back navigation occurs. The current implementation of the `onBackClick` directly calls `listDetailNavigator.navigateBack()` without first checking if navigation is allowed, leading to unexpected behavior.

I expected the back navigation to occur only when it is allowed, preventing any unintended navigation when the back button is tapped multiple times in quick succession. Specifically, the `onBackClick` should check `listDetailNavigator.canNavigateBack()` before calling `navigateBack()` to ensure that the back navigation is permitted.
```kotlin
topicScreen(
showBackButton = !listDetailNavigator.isListPaneVisible(),
onBackClick = {
if(listDetailNavigator.canNavigateBack()) {
listDetailNavigator.navigateBack()
}
},
onTopicClick = ::onTopicClickShowDetailPane,
)
```

In typical scenarios using a standard `navController`, this issue can be avoided by checking the lifecycle state of the `navBackStackEntry` before popping the back stack:
```kotlin
if (navController.currentBackStackEntry?.lifecycle?.currentState == Lifecycle.State.RESUMED) {
navController.popBackStack()
}
```

### Relevant logcat output

_No response_

### Code of Conduct

- [X] I agree to follow this project's Code of Conduct

Contributor guide

Open the contributing guide

Research direction

Start at the topicScreen entry point and inspect its onBackClick callback and the listDetailNavigator methods it uses. The change is complete when repeated back taps only navigate while canNavigateBack() allows it; no test file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.