Modal not closed by hardware back button

I believe the expected functionality (on android that has the hardware back button) would be the back button to close the modal, but the modal stays open and the navigation in background moves one step back. Had to manually override all modals to implement the functionality.

Cordova CLI: 6.3.1
Gulp version:  CLI version 3.9.1
Gulp local:
Ionic Framework Version: 2.0.0-rc.1 (2016-10-13)
Ionic CLI Version: 2.1.1
Ionic App Lib Version: 2.1.1
Ionic App Scripts Version: 0.0.34
Node Version: v4.6.0

Hi, why add global event listened?

For made what you want? It’s what I use for made this.

I’m facing this exact behavior.

Android HW back button closes the parent window (even the app) while the modal stays open. Shouldn’t it close the modal? I believe it to be same for Alerts and other “pop-ups”.

In any case, @ludovicevrard , I don’t see the relation with the event listener. Are you suggesting to capture the back button event? I am doing so with registerBackButtonAction but the modal does not appear to be in my tab navigation stack.

Yep, an issue exist in the Ionic’s GitHub

1 Like

@reedrichards thanks for pointing this out. The activePortal part of the laserus solution here solved my issues.

Thx for pointing that, that good be a workaround.
In my case I will still wait a while to see if the Ionic team comes with a fix in one of the next release (finger crossed ;))

I have solved this by registering a back button action in the modal constructor:

this.unregisterBackButtonAction = platform.registerBackButtonAction(() => {

and then unregistering it on modal dismissal:

dismiss(submit) {
1 Like

Is anyone still getting this issue? I’ve only just noticed it on one of my apps today. Basically the hardware back button fails to close/dismiss the modal, and instead navigates BACK for the page behind the modal.

Cordova CLI: 6.5.0 
Ionic Framework Version: 3.0.1
Ionic CLI Version: 2.2.2
Ionic App Lib Version: 2.2.1
Ionic App Scripts Version: 1.3.0
ios-deploy version: Not installed
ios-sim version: 5.0.13 
OS: macOS Sierra
Node Version: v6.10.2
Xcode version: Xcode 8.2.1 Build version 8C1002

Ionic 3 Try This Code it Works perfectly

          //Check Hardware Back Button Double Tap to Exit And Close Modal On Hardware Back
          let lastTimeBackPress = 0;
          let timePeriodToExit  = 2000;
          this.platform.registerBackButtonAction(() => {

              let activePortal = this.ionicApp._loadingPortal.getActive() || // Close If Any Loader Active
              this.ionicApp._modalPortal.getActive() ||  // Close If Any Modal Active
              this.ionicApp._overlayPortal.getActive(); // Close If Any Overlay Active

              if (activePortal) {
              else if(this.nav.canGoBack()){
                  //Double check to exit app
                  if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
                      this.platform.exitApp(); //Exit from app
                  } else {
                    this.toast.create("Press back button again to exit");
                    lastTimeBackPress = new Date().getTime();
1 Like

Thank you @conceps. But I have small problems
When any model opens, on first back key press it backs the Page and on second back press only Model closes.
and my second problem is that with same else code I am not getting second press option but app exit.

I think the Second Problem is you are not properly importing toast

import { ToastController } from 'ionic-angular';
constructor(public toastCtrl: ToastController) {

let toast = this.toastCtrl.create({
      message: 'Press back button again to exit',
      duration: 3000

I think the First Problem is modal dismiss check the if else condition properly based on your needs.
are you use this code in app.component.ts after platform ready.
Lets try it.

can you please tell me what is this.ionicApp here so as to access _overlayPortal as it is showing me error as

Property ‘ionicApp’ does not exist

I would imagine it is

import { App } from 'ionic-angular';
    private ionicApp:App
  ) {

but I don’t see _modalPortal property on it :open_mouth:

1 Like