Ionic Capacitor hardware back button is automatically closing the app
So, I feel a bit dumb after realizing this, but it is because I had to run the below commands, because I apparently didn't update them when upgrading Capacitor a while back. Make sure all of your plugins are fully updated, yours may be different than mine.
npm install @capacitor/appnpx cap sync
I had the same issue and I tried installing all the plugins like it says here
npm install @capacitor/app @capacitor/haptics @capacitor/keyboard @capacitor/status-bar
After I finished installing, I still got the error. Also my PushNotifications plugin was not working either.
My problem was I forgot to delete the OnCreate
method from MainActivity.java
. So if you still have the problem after installing the plugins, try to delete OnCreate
method so your MainActivity.java
looks like this:
public class MainActivity extends BridgeActivity {}
See more here.
It also fixed my PushNotifications plugin.
If I understand it correctly, if you have any plugin that is not registered correctly, it will cause this kind of error. This answer also helped me.
Install capacitor app.
npm install @capacitor/app
Import it.
import { App as CapacitorApp } from '@capacitor/app';
Add back listener if can go back then we can push to back or exit the app.
CapacitorApp.addListener('backButton', ({canGoBack}) => { if(!canGoBack){ CapacitorApp.exitApp(); } else { window.history.back(); }});