Search code examples
angularionic-frameworkmobilehardwareback

the Hardware Back Button does not work with Ionic and Angular


I haven't been able to get it to work for a few weeks now the Hardware Back Button. currently when we click back button the app is closed.

ionic info

this.platform.backButton.subscribeWithPriority(-1, () => {
  console.log("test")      
});

Solution

  • I found a solution :

    import { App } from '@capacitor/app';
        App.addListener('backButton', ({ canGoBack }) => {
         console.log(canGoBack);
          if(canGoBack){
           window.history.back();
           } else {
            App.exitApp();
           }
         });
    

    watch out, on android :

    registerPlugin(com.capacitorjs.plugins.app.AppPlugin.class);