diff --git a/apps/mobile/src/components/ui/AppBar.android.test.tsx b/apps/mobile/src/components/ui/AppBar.android.test.tsx
index 4aa9c755450f..510b63fa7d9a 100644
--- a/apps/mobile/src/components/ui/AppBar.android.test.tsx
+++ b/apps/mobile/src/components/ui/AppBar.android.test.tsx
@@ -2,6 +2,10 @@ import { act, type ReactNode } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, expect, it, vi } from "vite-plus/test";
+vi.mock("../../features/keyboard/dismissOpenKeyboard", () => ({
+ dismissOpenKeyboard: () => false,
+}));
+
vi.mock("react-native", () => ({
View: (props: { readonly children?: ReactNode }) =>
{props.children}
,
Keyboard: { dismiss: () => {} },
diff --git a/apps/mobile/src/components/ui/AppBar.android.tsx b/apps/mobile/src/components/ui/AppBar.android.tsx
index f647535a1d42..395256d9f3be 100644
--- a/apps/mobile/src/components/ui/AppBar.android.tsx
+++ b/apps/mobile/src/components/ui/AppBar.android.tsx
@@ -1,3 +1,4 @@
+import { dismissOpenKeyboard } from "../../features/keyboard/dismissOpenKeyboard";
import { type RefObject, use, useRef, useState } from "react";
import { Keyboard, type TextInputInstance, View } from "react-native";
import { useHardwareBack } from "../../lib/backHandler";
@@ -68,7 +69,7 @@ function SearchableAppBar(props: AppBarProps & { readonly search: AppBarSearch }
search.onClose?.();
};
useHardwareBack(searching && !inline && !controlled, () => {
- close();
+ if (!dismissOpenKeyboard()) close();
return true;
});
return ;
diff --git a/apps/mobile/src/features/keyboard/dismissOpenKeyboard.ts b/apps/mobile/src/features/keyboard/dismissOpenKeyboard.ts
new file mode 100644
index 000000000000..5ef93554b6e4
--- /dev/null
+++ b/apps/mobile/src/features/keyboard/dismissOpenKeyboard.ts
@@ -0,0 +1,18 @@
+import { KeyboardController, KeyboardEvents } from "react-native-keyboard-controller";
+
+let dismissPending = false;
+for (const event of ["keyboardWillShow", "keyboardDidShow", "keyboardDidHide"] as const) {
+ KeyboardEvents.addListener(event, () => {
+ dismissPending = false;
+ });
+}
+
+export function dismissOpenKeyboard(): boolean {
+ // The custom composer is a native EditText outside React Native's focus tracking.
+ if (!KeyboardController.isVisible()) return false;
+ // A missed hide event must not leave Back trapped behind a stale visible cache.
+ if (dismissPending) return false;
+ dismissPending = true;
+ void KeyboardController.dismiss();
+ return true;
+}
diff --git a/apps/mobile/src/features/keyboard/keyboardControllerState.test.tsx b/apps/mobile/src/features/keyboard/keyboardControllerState.test.tsx
index f628e495f9e2..7cdd3d858739 100644
--- a/apps/mobile/src/features/keyboard/keyboardControllerState.test.tsx
+++ b/apps/mobile/src/features/keyboard/keyboardControllerState.test.tsx
@@ -15,9 +15,20 @@ vi.mock("react-native-keyboard-controller/src/bindings", () => ({
return { remove: () => named.delete(listener) };
},
},
- KeyboardControllerNative: { dismiss: () => {} },
+ KeyboardControllerNative: { dismiss: vi.fn() },
}));
+vi.mock("react-native", () => ({
+ Keyboard: { isVisible: () => false, dismiss: vi.fn() },
+}));
+vi.mock("react-native-keyboard-controller", async () => {
+ const { KeyboardController } = await import("react-native-keyboard-controller/src/module");
+ const { KeyboardEvents } = await import("react-native-keyboard-controller/src/bindings");
+ return { KeyboardController, KeyboardEvents };
+});
+
+import { dismissOpenKeyboard } from "./dismissOpenKeyboard";
+import { KeyboardControllerNative } from "react-native-keyboard-controller/src/bindings";
import { useKeyboardState } from "react-native-keyboard-controller/src/hooks/useKeyboardState";
import { KeyboardController } from "react-native-keyboard-controller/src/module";
@@ -82,3 +93,29 @@ it("waits for the native hide before dismiss resolves after a didShow-only repai
emit("keyboardDidHide", 0);
await dismissal;
});
+
+it("dismisses the custom composer before Back navigates, including after resume", () => {
+ using _keyboard = setup();
+ const dismiss = vi.mocked(KeyboardControllerNative.dismiss);
+ dismiss.mockClear();
+
+ emit("keyboardDidShow", 376);
+ expect(dismissOpenKeyboard()).toBe(true);
+ expect(dismiss).toHaveBeenCalledTimes(1);
+ emit("keyboardDidHide", 0);
+ expect(dismissOpenKeyboard()).toBe(false);
+
+ emit("keyboardDidShow", 376);
+ expect(dismissOpenKeyboard()).toBe(true);
+ // A dropped hide event must not trap later Back presses in a stale visible cache.
+ expect(dismissOpenKeyboard()).toBe(false);
+ expect(dismiss).toHaveBeenCalledTimes(2);
+
+ emit("keyboardWillShow", 376);
+ expect(dismissOpenKeyboard()).toBe(true);
+ expect(dismissOpenKeyboard()).toBe(false);
+ emit("keyboardDidShow", 376);
+ expect(dismissOpenKeyboard()).toBe(true);
+ emit("keyboardDidHide", 0);
+ expect(dismissOpenKeyboard()).toBe(false);
+});
diff --git a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx
index 92d6be2d3309..574f2c21fc01 100644
--- a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx
+++ b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx
@@ -1,3 +1,4 @@
+import { dismissOpenKeyboard } from "../keyboard/dismissOpenKeyboard";
import {
WorkspaceColumnsRendererContext,
WorkspacePresentationHostContext,
@@ -812,7 +813,7 @@ function AdaptiveWorkspaceLayoutContent(
if (state && workspaceRoutesOf(state.routes.slice(0, state.index + 1)).overlays.length > 0) {
return false;
}
- setPrimarySidebarVisible(false);
+ if (!dismissOpenKeyboard()) setPrimarySidebarVisible(false);
return true;
});
return () => subscription.remove();
diff --git a/apps/mobile/src/features/layout/workspace/PullRequestToolbar.android.tsx b/apps/mobile/src/features/layout/workspace/PullRequestToolbar.android.tsx
index 332c1befe18b..23323c0817b3 100644
--- a/apps/mobile/src/features/layout/workspace/PullRequestToolbar.android.tsx
+++ b/apps/mobile/src/features/layout/workspace/PullRequestToolbar.android.tsx
@@ -1,3 +1,4 @@
+import { dismissOpenKeyboard } from "../../keyboard/dismissOpenKeyboard";
import { useNavigation } from "@react-navigation/native";
import { useEffect } from "react";
import { BackHandler, Keyboard } from "react-native";
@@ -46,8 +47,7 @@ export function PullRequestToolbar(props: {
const state = navigation.getState();
if (!state || state.routes[state.index]?.name !== "Home" || !isTabActive("pullRequests"))
return false;
- if (Keyboard.isVisible()) Keyboard.dismiss();
- else closeSearch();
+ if (!dismissOpenKeyboard()) closeSearch();
return true;
});
return () => subscription.remove();
diff --git a/apps/mobile/src/features/layout/workspace/WorkspaceListHeader.android.tsx b/apps/mobile/src/features/layout/workspace/WorkspaceListHeader.android.tsx
index 5c6f79358415..9f36989a1e6c 100644
--- a/apps/mobile/src/features/layout/workspace/WorkspaceListHeader.android.tsx
+++ b/apps/mobile/src/features/layout/workspace/WorkspaceListHeader.android.tsx
@@ -1,3 +1,4 @@
+import { dismissOpenKeyboard } from "../../keyboard/dismissOpenKeyboard";
import { useEffect, useState } from "react";
import { useNavigation, useNavigationState } from "@react-navigation/native";
import { BackHandler, Keyboard } from "react-native";
@@ -49,8 +50,7 @@ export function WorkspaceListHeader(props: WorkspaceListHeaderProps) {
isTabActive?.("threads") === false
)
return false;
- if (Keyboard.isVisible()) Keyboard.dismiss();
- else closeSearch();
+ if (!dismissOpenKeyboard()) closeSearch();
return true;
});
return () => subscription.remove();
diff --git a/apps/mobile/src/features/layout/workspace/useAndroidWorkspaceNavigation.ts b/apps/mobile/src/features/layout/workspace/useAndroidWorkspaceNavigation.ts
index dfc8e13d162c..e5b3d3005588 100644
--- a/apps/mobile/src/features/layout/workspace/useAndroidWorkspaceNavigation.ts
+++ b/apps/mobile/src/features/layout/workspace/useAndroidWorkspaceNavigation.ts
@@ -1,6 +1,7 @@
+import { dismissOpenKeyboard } from "../../keyboard/dismissOpenKeyboard";
import { StackActions, useFocusEffect, useNavigation } from "@react-navigation/native";
import { useCallback } from "react";
-import { BackHandler, Keyboard, Platform } from "react-native";
+import { BackHandler, Platform } from "react-native";
import {
resetAndroidWorkspace,
returnToAndroidWorkspaceList,
@@ -39,8 +40,7 @@ export function useAndroidWorkspaceBack(tab: WorkspaceTab) {
if (Platform.OS !== "android") return;
dispatch?.({ type: "select", tab });
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
- if (Keyboard.isVisible()) Keyboard.dismiss();
- else goBack();
+ if (!dismissOpenKeyboard()) goBack();
return true;
});
return () => subscription.remove();