How to implement hardware back button in Ionic with Capacitor
Md Riyazuddin
Verified
When running in a Capacitor or Cordova application, Ionic Framework will emit an ionBackButton event on the user presses the hardware back button.
import { IonRouterOutlet, Platform } from '@ionic/angular';
import { App } from '@capacitor/app';
...
constructor(
private platform: Platform,
private routerOutlet: IonRouterOutlet
) {
this.platform.backButton.subscribeWithPriority(-1, () => {
if (!this.routerOutlet.canGoBack()) {
App.exitApp();
}
});
}
When listening for the ionBackButton event, you can register a handler to be fired. This handler can perform actions such as quitting the app or opening a confirmation dialog with the help of code.
Comments
Leave a Comment