Hardware Back Button Issue

When the App is loaded, then click on the android hardware button this.platform.backbutton.subscribeWithPriority(0, async () => {}); is not getting triggered for the first time and the app gets closed. when we tap or double tap on the screen then click on the android hardware button is getting triggered. if we navigate between the pages also it works fine.

Please help me with the solution.

Show me your code. So that I can give you a solution. For your reference: I am working on android project using ionic framework and angular. I encountered the error. Then I know that for subscriptionwithpriority method, the first parameter must be higher value. So that it will be your latest backbutton observer. Use the below code the override the default backbutton action.

this.platform.backbutton.subscribeWithPriority(9999, async () => {});

I tried with a high priority of 9999 is also having the same issue.

this.platform.ready().then(() => {
this.platform.backButton.subscribeWithPriority(0, async () => {
if (this.router.url == “/login” || this.router.url == “/home” || this.router.url == “/starter”) {
let dataValue = await this.autocloseOverlaysService.trigger();
if (dataValue) {

      }
      else {
        this.showAlert();
      }

    }
    else if (this.routerOutlet.canGoBack()) {
      let dataValue = await this.autocloseOverlaysService.trigger();
      if (dataValue) {

      }
      else {
        this.navCtrl.back();
      }
    }
  });
});

}

If i tap on screen it’s works fine.

@prudhvirajdandi Are sure, your autocloseOverlayService working properly. Did you checked for any console errors? In my point of view, You used if else statement. It always triggers else and exits. Use

console.log(suspected variable);

to print and find whats going on . I think the overlayclose service is the culprit.

no console errors. it’s a working code in other project.

App is getting closed on back button, it’s not triggering the backbutton functionality. if i tap on screen it works.

1 Like

Hey I also encountered this type of problem in one of my pages in my current project. Then I concluded that in android device the data got from api is such bulk to handle by the app. It was physically overloaded. The weird thing is it works fine in dev server. But in android device it shows only white screen at start up. After tapping, it enter into the expected format. If this is the case, probably your page is physically overloaded.
Try any api request with some delay in you startup (if you have).

ionViewDidEnter(){
this.platform.ready.then(()=>{
setTImeout(()=>{
//api request  or closeoverlayservice backbutton subscription
},500);
});
}

Still the same issue.

2 Likes

Finally, I moved from Cordova to Capacitor, Now it’s fine.