Utilizei o seguinte código:
import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core';
import { Location } from '@angular/common';
import { Platform, AlertController, IonRouterOutlet } from '@ionic/angular';
import { AppUrlOpen, Plugins } from '@capacitor/core';
const { App } = Plugins;
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {
@ViewChild(IonRouterOutlet, { static: true }) routerOutlet: IonRouterOutlet;
...
constructor(
private platform: Platform,
private _location: Location,
private alertCtrl: AlertController,
)
{
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.platform.backButton.subscribeWithPriority(10, () => {
if (!this.routerOutlet.canGoBack()) {
this.showExitConfirm();
}
else {
this._location.back();
}
});
});
}
showExitConfirm() {
this.alertCtrl.create({
header: 'App termination',
message: 'Do you want to close the app?',
backdropDismiss: false,
buttons: [{
text: 'Stay',
role: 'cancel',
handler: () => {
console.log('Application exit prevented!');
}
}, {
text: 'Exit',
handler: () => {
//navigator['app'].exitApp();
App.exitApp();
}
}]
})
.then(alert => {
alert.present();
});
}
}
O app é publicado na nuvem e acessado pelo celular como um PWA. Não consigo de maneira alguma capturar o evento do backbutton (ao clicar no botão voltar do Android) para mostrar o alerta.
Pergunta: é uma limitação do Ionic ? Tem como funcionar ?