Skip to content

Commit ecdbcee

Browse files
committed
fix(core): add fallback handler for hardware back button when no router is present
1 parent 29f3f72 commit ecdbcee

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

core/src/utils/hardware-back-button.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { win } from '@utils/browser';
21
import type { CloseWatcher } from '@utils/browser';
2+
import { win } from '@utils/browser';
33
import { printIonError } from '@utils/logging';
44

55
import { config } from '../global/config';
@@ -69,6 +69,21 @@ export const startHardwareBackButton = () => {
6969
});
7070
doc.dispatchEvent(ev);
7171

72+
/**
73+
* If no handlers have been registered, fall back to the default
74+
* behavior of navigating back in history. This ensures the hardware
75+
* back button works even when no router or custom handler is present.
76+
*/
77+
if (handlers.length === 0) {
78+
handlers.push({
79+
priority: FALLBACK_BACK_BUTTON_PRIORITY,
80+
handler: () => {
81+
win?.history.back();
82+
},
83+
id: index++,
84+
});
85+
}
86+
7287
const executeAction = async (handlerRegister: HandlerRegister | undefined) => {
7388
try {
7489
if (handlerRegister?.handler) {
@@ -138,3 +153,4 @@ export const startHardwareBackButton = () => {
138153

139154
export const OVERLAY_BACK_BUTTON_PRIORITY = 100;
140155
export const MENU_BACK_BUTTON_PRIORITY = 99; // 1 less than overlay priority since menu is displayed behind overlays
156+
const FALLBACK_BACK_BUTTON_PRIORITY = -1; // Fallback when no other handlers are registered

0 commit comments

Comments
 (0)