ionic-framework: Ionic 2: Modal window doesn't dismiss when overriding back button on Android device.
Short description of the problem:
If you override the back button e.g.:
registerBackButtonListener() {
this.platform.registerBackButtonAction(() => {
var nav = this.getNav();
if (nav.canGoBack()) {
nav.pop();
}
else {
//... do something else...
}
});
}
If a modal window is open, it is not closed and instead the page in the background will navigate back.
What behavior are you expecting?
The line: nav.canGoBack() should pop a modal view, not the page underneath.
Which Ionic Version? 1.x or 2.x 2
Run ionic info from terminal/cmd prompt: (paste output below)
Your system information:
Cordova CLI: 6.1.1 Ionic Framework Version: 2.0.0-beta.9 Ionic CLI Version: 2.0.0-beta.25 Ionic App Lib Version: 2.0.0-beta.15 ios-deploy version: 1.8.6 ios-sim version: 5.0.8 OS: Mac OS X El Capitan Node Version: v4.3.0 Xcode version: Xcode 7.2.1 Build version 7C1002
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 12
- Comments: 44 (5 by maintainers)
In the end I have this for my back button:
OK, I needed it and have a workaround:
Just dismiss portals yourself:
For testing I used html windows and keypress event, but you can define this function for registering back button. There is ready param, which do not try to dismiss portal if we in process of dismiss…
If this can help, that’s what I have for the current Ionic 2 version, based on @laserus 's code. My use case here is that I want the user to be directed back to the home page if they have clicked a menu item.
Hi guys, I got it working perfectly for multiple tabs, modals, menu , keyboard and other overlays with help from all the above comments. Here’s the working code for Ionic 3.0.0:
` this.platform.registerBackButtonAction(() => {
This should work out all possible scenarios, let me know if somethig is amiss.
Hi, what was the result of the discussion with your team? Is there a consensus on how to override the back button without losing basic page back, modal dismissing and alert dismissing functionality?
Maybe a good idea is to introduce new event
beforeAppClose? In which You couldreturn false;to prevent from closing?@laserus solution works like charm thanx buddy
thanks to all!!
Thanks, will do!
@laserus thanks. works fine for me!
@laserus thanks for posting your workaround. The activePortal part solved my issues with modals.
@lazy-ape I think currently there is a bug https://github.com/driftyco/ionic/issues/8692 that modal is not closing. So you should treat it outside of app.navPop(), but otherwise it is good.
Simplest way is to do
this.ionicApp._modalPortal.getActive()and dismiss it yourself.I have a similar approach (without modals yet) but instead of
if (page instanceof TabsPage) {I put on pages confirmation variable “needConfirmationOnExit” (but more appropriate approach is to use some base class and extend).