-
Notifications
You must be signed in to change notification settings - Fork 13.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ionic 2: Modal window doesn't dismiss when overriding back button on Android device. #6982
Comments
Hi guys, can someone tell me if I'm the problem here or if it's the framework? I just want to override the back button to catch app exiting, so if I'm doing something wrong, please tell me. |
Hello @daveshirman ! Thanks for opening an issue with us. A modal is actually an instance of
Thanks for using Ionic and sorry for the delay on this! |
Hi, thanks for your reply. But how does this actually solve the problem? I mean, how do I know programmatically whether I need to This seems like something the platform should supply (already does?). |
Sorry for the misunderstanding! If you would like to catch a user closing your app you can simply listen for the pause cordova event. Hope that explains things better! |
Hello, i am currently discussing this with my team. Also sorry for misunderstanding your issue, i understood the issue to be about catching an app exiting, my mistake. |
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? |
Hi, can someone please reply to me from the Ionic team? Thanks. |
Hello @daveshirman ! Sorry for the delay on this issue, with the release of beta.10 last week i have been very busy with things related to the release. I plan on taking another look at this issue today. Thanks! |
Hey @daveshirman at this time there is not a good, clean way to programatically detect if their is an overlay component (like modals or alerts) open (although it is something we are considering). For a workaround i would try something like
Sorry for any hassle this causes, i will be closing this issue for now as it is not something that we plan on implementing soon, but will reopen this issue if something changes. Thanks for using Ionic, and again, sorry for the delay on this. |
@daveshirman Did you ever find a good solution to this issue? We have been running into this issue too. Very annoying. |
Hi, unfortunately not. My "solution"was to abandon overriding the back Don't know when/whether the guys at Ionic are intending to fix it either. On 10 Sep 2016 2:02 a.m., "Chuckv01" [email protected] wrote:
|
I had the same problem. My "solution" was: if their is an overlay component, the ion-app element has the class "disable-scroll", so i can check if the class if present or not to prevent go back. Something like this:
I know that is not a beautiful solution but it works. In the confirm method I show the confirm modal. Also I used a flag to prevent that users press back repeatedly and multiples modal appear. |
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... |
@laserus that looks promising. I will have to try it out. @jgw96 This is still an issue for us as of RC1. I don't think the issue should be closed. We've been forced to stop overriding the android back button as modals and dialogues still do not behave properly when it is overridden and the hardware back button is pressed. |
Hi, I find a way to solve it in
So, I override the back button to catch app exiting like this:
My english is pool. Hope it can help you. |
@lazy-ape I think currently there is a bug #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 I have a similar approach (without modals yet) but instead of
|
@laserus thanks for posting your workaround. The activePortal part solved my issues with modals. |
@laserus thanks. works fine for me! |
Greatly helped! Thanks a lot. |
I see that others are using my piece of code and want to warn about one caveat: the onDidDismiss callback is only one per modal... so I had one inside one component that returned value from modal an pushed it to observer and then complete the observer (memory leak). And this one in backbuttonhandler which doing nothing atm. And this one broke the whole app logic flow. To solve it, I had to made some changes to it, but I guess onDidDismiss could completely be removed here.... need to be tested (if it affects your app) |
Newby question... Where do I implement this code? Do I need it on every window, or can I add it once globally somewhere? |
@PurpleEdge2214 you can put it once in bootstrap app.component.ts (or whatever is your first component). Inside constructor. |
Thanks, will do! |
@laserus from where to import those services ConfigService,NfcService etc.. I found error when using those in constructor, Thanks. |
@sarfraaz53 You should remove them from your code. That is piece of my code with my services. You do not need them... I just shown constructor function in order to show where you place the code, ignore everything unknown. |
@laserus - in your comment about the caveat in your fix you said "To solve it, I had to made some changes to it, but I guess onDidDismiss could completely be removed here.... need to be tested". What exactly did you have to change to solve the onDidDismiss issue? I am having the same problem - once I dismiss the modal once with the back button, no modals show after that. Could you perhaps give some ideas? |
@hayuki just remove:
|
If this can help, that's what I have for the current Ionic 2 version, based on @laserus 's code.
|
Hey guys, here is my final solution, thanks @laserus for your help!
|
This solution was working great up until a recent update (2.2.0+):
Now activePortal is always undefined even if I have modals open. I don't believe anything else has changed in my app. Anyone else encountering this? |
@gentlemanjohn Judging from the code it should work. If you open
And next they are referenced as:
|
thanks to all!! |
You can run this app:
https://github.com/yanxiaojun617/ionic2_tabs/
At 2017-04-17 04:18:26,"John Murphy" <[email protected]> wrote:
This solution was working great up until the recent update (3.0.0 +):
let activePortal = ionicApp._loadingPortal.getActive() ||
ionicApp._modalPortal.getActive() ||
ionicApp._toastPortal.getActive() ||
ionicApp._overlayPortal.getActive();
if (activePortal) {
activePortal.dismiss();
}
Now activePortal is always undefined even I have modals open. I don't believe anything else has changed in my app. Anyone else encountering this?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
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 should work out all possible scenarios, let me know if somethig is amiss. |
@laserus solution works like charm thanx buddy |
@mayurmarwa your code seems nice but I don't understand the two lines: let view = this.nav.getActive(); // As none of the above have occurred, its either a page pushed from menu or tab
let activeVC = this.nav.getActive(); //get the active view
... same variable attribution.. |
Maybe a good idea is to introduce new event |
//Check Hardware Back Button Double Tap to Exit And Close Modal On Hardware Back
|
Found a solution by creating a provider with an observable. // BACK PROVIDER constructor(public http: HttpClient) { setBack(val){ getBack(){ // ANY CLASSIC PAGE back(){ ionViewWillEnter(){ autoRegisterBack(){ // MODAL PAGE ionViewWillEnter(){ dismiss(){ |
Thanks Man, Helped Again @laserus |
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out. |
Short description of the problem:
If you override the back button e.g.:
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
The text was updated successfully, but these errors were encountered: