Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use local scope variable for backButton event handler #1932

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/1932.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix physical back button behavior on Android
11 changes: 3 additions & 8 deletions src/app/components/Ionic/IonicProvider.tsx
Original file line number Diff line number Diff line change
@@ -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>(undefined)

let backButtonListenerHandle: PluginListenerHandle | undefined

const IonicContextProvider: FC<PropsWithChildren> = ({ children }) => {
const navigate = useNavigate()

Expand All @@ -15,7 +13,7 @@ const IonicContextProvider: FC<PropsWithChildren> = ({ 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 {
Expand All @@ -24,10 +22,7 @@ const IonicContextProvider: FC<PropsWithChildren> = ({ children }) => {
})

return () => {
if (backButtonListenerHandle) {
backButtonListenerHandle.remove()
backButtonListenerHandle = undefined
}
backButtonListenerHandle.remove()
}
}, [navigate])

Expand Down
Loading