If in the root, close the application, UNLESS the user seems to have been spamming the back button (500ms delay)
E.g. if the user is in a nav stack with 3 pages, spams the back button to get back to the first one, the app wont close when he makes it to the last page. If he waits 500ms and clicks back again, it closes the app.
I hope I understood the question correctly and that this can be of use!
Edit 2: But yeah, if you want the app to exit when clicking twice, change the > 500 to < 500, 500 being the duration/delay. Though I personally think the behavior of not closing the app when the back button is being spammed is much better.
Edit 3: If you want to keep both the “don’t close on spam” and “close with 2 taps”, use this
But the purpose of the lastBack is to see when the button was last pressed, and if it was pressed within the past 500ms we don’t want to show the toast or exit the app, in case they were spamming the back button to just go back to the root So it needs to be set after each press, otherwise it’s pretty much pointless to have it at all. I suppose it could be renamed to lastPress or something, cause it doesn’t actually resemble when we last went back a page, but when the last press was in general.
Found your answer and took the liberty to change it and perhaps make a little neater
It worked perfectly for my use case… thanks.
Here we go…
platform.registerBackButtonAction(() => {
// getActiveNav is being deprecated so, fetch the first available nav.
const nav = app.getActiveNavs()[0];
const active = nav.getActive();
let closeDelay = 2000;
let spamDelay = 500;
if (active.isOverlay) {
// Checks if is dismissing something..
// avoid exceptions if user spams back-button and stack isn't finished dismissing view yet.
if (!this.dismissing) {
active.dismiss().then(() => this.dismissing = false);
}
this.dismissing = true;
} else if (((Date.now() - this.lastBack) < closeDelay) &&
(Date.now() - this.lastBack) > spamDelay) {
// Leaves app if user pressed button not faster than 500ms a time,
// and not slower than 2000ms a time.
platform.exitApp();
} else {
if (!this.spamming) { // avoids multiple toasts caused by button spam.
let t = toast.create({
message: "Pressione novamente para sair..",
duration: closeDelay,
dismissOnPageChange: true
});
t.onDidDismiss(() => this.spamming = false);
t.present();
}
this.spamming = true;
}
this.lastBack = Date.now();
});
Hi @mich356c,
The solution which you provided is not working for me. My use case is, I have an app where I have used ion-tabs for creating tabs for 4 different pages. And on these 4 pages, I have several pages which I open as a modal (which is an overlay and not a page, so can’t pop it). So, on using your code, if I click back button on any of 4 pages, it presents me the “Alert box to exit app” (works just fine), but when I open any modal on any pages and click back button to close it, it just don’t work. Also, after clicking the back button on modal, and then coming back to page and then clicking again the back button doesn’t brings the “Alert box to exit app” (which was working fine before I open any modal).
I am really stuck in all this, I really didn’t find any good solution which can close my modals on clicking back button and brings “Alert box to exit app” on clicking back button on root page.
Please help