The best way to control Claude Code, Codex and more from any computer or phone
fix(mobile): a replaced Android confirm or prompt runs its cancel (#39138)
Wout Stiens committed
Nov 19, 2026 at 15:34 UTC
3dff4daf14595c0b9f1284a5319a6638107febd7
2 files changed
+64
-13
apps/mobile/src/components/ui/Dialog.android.test.tsx
+51
-4
@@ -90,13 +90,60 @@ it("keeps a dialog raised while the activity is replaced", () => {
90
expect(current.container.textContent).toContain("Shared content imported");
91
});
92
93
-it("tells a replaced alert it was dismissed", () => {
93
+it.each([
94
+ {
95
+ kind: "alert",
96
+ present: (onClose: () => void) =>
97
+ Dialog.alert("Older", undefined, [{ text: "OK" }], { onDismiss: onClose }),
98
+ },
99
+ {
100
+ kind: "confirm",
101
+ present: (onClose: () => void) =>
102
+ Dialog.confirm({
103
+ title: "Remove project",
104
+ confirmText: "Remove",
105
+ onConfirm: () => {},
106
+ onCancel: onClose,
107
+ }),
108
+ },
109
+ {
110
+ kind: "text-input",
111
+ present: (onClose: () => void) =>
112
+ Dialog.prompt({
113
+ title: "Rename",
114
+ initialValue: "",
115
+ confirmText: "Save",
116
+ onConfirm: () => {},
117
+ onCancel: onClose,
118
+ }),
119
+ },
120
+])("tells a replaced $kind it was closed without an answer", ({ present }) => {
121
mountHost();
95
- const onDismiss = vi.fn();
96
- act(() => Dialog.alert("Older", undefined, [{ text: "OK" }], { onDismiss }));
122
+ const onClose = vi.fn();
123
+ act(() => present(onClose));
124
act(() => Dialog.alert("Newer", undefined, [{ text: "OK" }]));
125
99
- expect(onDismiss).toHaveBeenCalledOnce();
126
+ expect(onClose).toHaveBeenCalledOnce();
127
+});
128
+
129
+it.each([
130
+ { action: "confirm", clickIndex: -1 },
131
+ { action: "cancel", clickIndex: -2 },
132
+])("ignores a late $action click from a replaced confirmation", ({ clickIndex }) => {
133
+ const { container } = mountHost();
134
+ const onConfirm = vi.fn();
135
+ const onCancel = vi.fn();
136
+ act(() =>
137
+ Dialog.confirm({ title: "Remove project", confirmText: "Remove", onConfirm, onCancel }),
138
+ );
139
+ const lateClick = renderedClicks.at(clickIndex)!;
140
+ act(() => Dialog.alert("Newer", undefined, [{ text: "OK" }]));
141
+
142
+ act(() => lateClick());
143
+
144
+ expect(onConfirm).not.toHaveBeenCalled();
145
+ expect(onCancel).toHaveBeenCalledOnce();
146
+ expect(container.textContent).toContain("Newer");
147
});
148
149
it("ignores a late action from a dialog that a newer one replaced", () => {
apps/mobile/src/components/ui/Dialog.android.tsx
+13
-9
@@ -53,13 +53,20 @@ function setPresented(next: PresentedDialog | null) {
53
function submitRequest(dialog: DialogRequest) {
54
const replaced = presented;
55
setPresented({ ...dialog, id: ++nextPresentationId });
56
- // A replaced alert closed without an answer, which its owner hears as a dismissal.
57
- if (replaced?.kind === "alert") replaced.request.options?.onDismiss?.();
56
+ // A replaced dialog closed without an answer, which its owner hears as a dismissal.
57
+ if (replaced) closeUnanswered(replaced);
58
+}
59
+
60
+function closeUnanswered(dialog: DialogRequest) {
61
+ if (dialog.kind === "alert") dialog.request.options?.onDismiss?.();
62
+ else dialog.request.onCancel?.();
63
}
64
65
/** Closes the dialog only if it is still the one the action came from. */
66
function dismiss(id: number) {
62
- if (presented?.id === id) setPresented(null);
67
+ if (presented?.id !== id) return false;
68
+ setPresented(null);
69
+ return true;
70
}
71
72
function subscribe(listener: () => void) {
@@ -97,15 +104,12 @@ export function Host() {
104
const presented = useSyncExternalStore(subscribe, getPresented, getPresented);
105
106
const handleCancel = () => {
100
- if (!presented) return;
101
- dismiss(presented.id);
102
- if (presented.kind === "alert") presented.request.options?.onDismiss?.();
103
- else presented.request.onCancel?.();
107
+ if (!presented || !dismiss(presented.id)) return;
108
+ closeUnanswered(presented);
109
};
110
111
const handleConfirm = (nativeInputValue?: string) => {
107
- if (!presented) return;
108
- dismiss(presented.id);
112
+ if (!presented || !dismiss(presented.id)) return;
113
if (presented.kind === "confirm") {
114
presented.request.onConfirm();
115
} else if (presented.kind === "text-input") {