fix(ui): forward Back when the handler's enabled state is stale

BackHandler's enabled flag trails the state it is derived from by a
dispatch and a recomposition, so a second quick press after the last
overlay closes still reaches this handler. handleBackPressed then finds
nothing to unwind and returns false, and dropping that result swallowed
the press instead of leaving the chat screen.

The result is now forwarded: pop a route if one is beneath, and otherwise
finish, which is what the press would have done had the handler already
been disabled. This mirrors the guard the callback carried before the
Navigation 3 host replaced it.

Not covered by a test. The condition is a race between input dispatch and
recomposition inside a composed hierarchy, and this project cannot host a
Compose unit test yet: isIncludeAndroidResources fails 24 existing tests
with "targetSdkVersion=37 > maxSdkVersion=35", and pinning Robolectric to
sdk 35 fails 109.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LgmKvwg6yjo1cEmagppK9m
This commit is contained in:
Moe Hamade 2026-09-08 21:54:48 +03:00
parent 664c2dac46
commit 3bdaf2ae59

View File

@ -220,7 +220,11 @@ class MainActivity : OrientationAwareActivity() {
BackHandler(
enabled = navigator.backStack.lastOrNull() == ChatRoute && canHandleBack
) {
chatViewModel.handleBackPressed()
// enabled trails the state by a dispatch and a recomposition, so a
// second quick press can arrive with nothing left to unwind. Forward
// it rather than swallowing it: pop a route if there is one, and
// otherwise leave, which is what the press would have done anyway.
if (!chatViewModel.handleBackPressed() && !navigator.goBack()) finish()
}
}
}