ChatGPT just told me about all of these “gotchas” to do with using Ionic Framework and Capacitor on Android:
3. Common mistakes
| Mistake |
Result |
Only using Ionic’s ion-router-outletnavigation |
Back button doesn’t trigger Ionic navigation |
Not subscribing to App.addListener('backButton') |
WebView never sees Android back event |
Forgetting App.exitApp() |
App freezes on root page |
Is this actually accurate?
It’s very surprising to me that neither ion-router-outlet nor capacitor capture the back button by default by themselves. Why was that decision made?
1 Like
I would say that is wrong. The Android back button and back gesture by default go back in an Ionic app on Android.
It wasn’t working for me until I added a little event listening effect
const ionRouter = useIonRouter();
useEffect(() => {
const handler = CapacitorApp.addListener('backButton', () => {
if (ionRouter.canGoBack()) {
ionRouter.goBack();
}
});
return () => {
handler.then(h => h.remove());
};
}, [ionRouter]);
before adding that, the back button in the android app was quitting the app, even though everything in the app is set up and linked with ionic router
Not sure, maybe you have something configured wrong.
See Hardware Back Button for Capacitor & Cordova on Android Devices
By default in Ionic, when the back button is pressed, the current view will be popped off the navigation stack, and the previous view will be displayed. If no previous view exists in the navigation stack, nothing will happen.