Skip to content

Commit

Permalink
Merge pull request #1932 from oasisprotocol/ml/android-back-button-fo…
Browse files Browse the repository at this point in the history
…llow-up

Use local scope variable for backButton event handler
  • Loading branch information
lubej authored May 13, 2024
2 parents 79e7d47 + 44ac7d8 commit cdc95f9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
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

0 comments on commit cdc95f9

Please sign in to comment.