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])