App destroyed after back button pressed

I Used IONIC 5 and ANgular 11 ,

When I click on the Bakc button the application close automatically

I see only this 3 lines in the logs :

 13:31:30.348 8980-8980/xxxxxxxxxxxx D/Capacitor: App paused
 13:31:30.966 8980-8980/xxxxxxxxxxxx D/Capacitor: App stopped
 13:31:30.973 8980-8980/xxxxxxxxxxxx D/Capacitor: App destroyed

Ionic INFO :

Ionic:

Ionic CLI : 6.16.3
Ionic Framework : @ionic/angular 5.6.11
@angular-devkit/build-angular : 0.1102.14
@angular-devkit/schematics : 11.2.13
@angular/cli : 11.2.13
@ionic/angular-toolkit : 3.1.1

Capacitor:

Capacitor CLI : 3.1.1
@capacitor/android : 3.1.1
@capacitor/core : 3.1.1
@capacitor/ios : not installed

Utility:

cordova-res : not installed globally
native-run : 1.4.0

System:

NodeJS : v12.22.1 (C:\Program Files\nodejs\node.exe)
npm : 6.14.12
OS : Windows 10

1 Like

Try the following:

app.component.ts

constructor(
     public platform: Platform
) {
     this.platform.ready().then(() => {
          this.platform.backButton.subscribeWithPriority(9999, () => {
               //Disable default backbutton behavior
               document.addEventListener("backbutton", function(event) {
                    event.preventDefault(); event.stopPropagation();
               }, false);

               //YOUR CUSTOM BACKBUTTON METHOD
               //EXMPLE:
               if (this.platform.is("android")) {
                    navigator["app"].exitApp();
               });
     });
}
1 Like

Hello, I had a similar error, it happened to me after updating capacitor to version 3.4.3.

I fixed it by running the following commands:

npm install @capacitor/app
npx cap sync

Apparently when update was not done well. Iā€™m leaving this here because this helped me and maybe it will help someone else.

Here I found the solution