From 68361d949f4bcc6eae06005b7e321c8cc0d4577c Mon Sep 17 00:00:00 2001 From: Moe Hamade <69801237+moehamade@users.noreply.github.com> Date: Wed, 26 Aug 2026 00:11:54 +0300 Subject: [PATCH] fix(ui): forward Back when canHandleBack is momentarily stale BackHandler's enabled flag trails the state it mirrors by a coroutine dispatch and a recomposition. A second Back press inside that window finds the handler still enabled while handleBackPressed() already has nothing to unwind, and ignoring its result consumed the press instead of letting the system act on it. The callback the handler replaced did forward it. Falls back to finish() on an unhandled press, which is what the dispatcher reached before once no enabled callback consumed it. Reported by Codex review on #912. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01MdKgoKZbL5K1wsn1WsES26 --- app/src/main/java/com/bitchat/android/MainActivity.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/bitchat/android/MainActivity.kt b/app/src/main/java/com/bitchat/android/MainActivity.kt index 8f0b84c1..aa336810 100644 --- a/app/src/main/java/com/bitchat/android/MainActivity.kt +++ b/app/src/main/java/com/bitchat/android/MainActivity.kt @@ -318,7 +318,11 @@ class MainActivity : OrientationAwareActivity() { // through to the system, which exits the app. val canHandleBack by chatViewModel.canHandleBack.collectAsState() BackHandler(enabled = canHandleBack) { - chatViewModel.handleBackPressed() + // enabled reaches this handler a dispatch and a recomposition + // after the state changes, so a second press can arrive while + // it is still true but there is no longer anything to unwind. + // Forward that press instead of swallowing it. + if (!chatViewModel.handleBackPressed()) finish() } ChatScreen(viewModel = chatViewModel) }