Search code examples
ionic3popoverback-button

PopOverPage does not close on back button


I am implementing an Android App with Ionic 3 and I use this guide to create a PopOverPage or menu lets say. Everything works fine except that when the user has clicked the pop over it remains open even if you click the back button which takes you to the previous page. Can anyone please help me using this guide to modify it a little bit to allow to close the pop over when the back button is pressed?


Solution

  • The below code should be written on the page on which you are calling the popover:

    import { IonicApp } from 'ionic-angular';
    
    class Something {
    
    constructor( private ionicApp: IonicApp) {}
    
     ionViewWillLeave() {
    
      this.closeModals();  
    }
    
       private closeModals() {
    
          let activePortal = this.ionicApp._loadingPortal.getActive() ||
          this.ionicApp._modalPortal.getActive() ||
          this.ionicApp._toastPortal.getActive() ||
          this.ionicApp._overlayPortal.getActive();
    
        if (activePortal) {
          activePortal.dismiss();
        }
      }
    
    }