From 44ac7d802ddca8c106ba9c93c46e49b41e8a7410 Mon Sep 17 00:00:00 2001 From: Matej Lubej Date: Mon, 13 May 2024 17:58:53 +0200 Subject: [PATCH] Use local scope variable for backButton event handler --- .changelog/1932.bugfix.md | 1 + src/app/components/Ionic/IonicProvider.tsx | 11 +++-------- 2 files changed, 4 insertions(+), 8 deletions(-) create mode 100644 .changelog/1932.bugfix.md diff --git a/.changelog/1932.bugfix.md b/.changelog/1932.bugfix.md new file mode 100644 index 0000000000..35279a020f --- /dev/null +++ b/.changelog/1932.bugfix.md @@ -0,0 +1 @@ +Fix physical back button behavior on Android diff --git a/src/app/components/Ionic/IonicProvider.tsx b/src/app/components/Ionic/IonicProvider.tsx index 1714d9a26d..c270d3ee00 100644 --- a/src/app/components/Ionic/IonicProvider.tsx +++ b/src/app/components/Ionic/IonicProvider.tsx @@ -1,12 +1,10 @@ import { createContext, FC, PropsWithChildren, useEffect } from 'react' import { useNavigate } from 'react-router-dom' -import { Capacitor, type PluginListenerHandle } from '@capacitor/core' +import { Capacitor } from '@capacitor/core' import { App } from '@capacitor/app' const IonicContext = createContext(undefined) -let backButtonListenerHandle: PluginListenerHandle | undefined - const IonicContextProvider: FC = ({ children }) => { const navigate = useNavigate() @@ -15,7 +13,7 @@ const IonicContextProvider: FC = ({ children }) => { * The back button refers to the physical back button on an Android device and should not be confused * with either the browser back button or ion-back-button. */ - backButtonListenerHandle = App.addListener('backButton', ({ canGoBack }) => { + const backButtonListenerHandle = App.addListener('backButton', ({ canGoBack }) => { if (!canGoBack) { App.exitApp() } else { @@ -24,10 +22,7 @@ const IonicContextProvider: FC = ({ children }) => { }) return () => { - if (backButtonListenerHandle) { - backButtonListenerHandle.remove() - backButtonListenerHandle = undefined - } + backButtonListenerHandle.remove() } }, [navigate])