How to hold back twice to exit

Well, now it doesn’t set this.lastBack in order to prevent the app from being closed when the back button is being spammed, because of the returns :wink:

Edit: Cleaned it up and using this myself now

platform.registerBackButtonAction(() => {
	const overlay = this.app._appRoot._overlayPortal.getActive();
	const nav = this.app.getActiveNav();

	if(overlay && overlay.dismiss) {
		overlay.dismiss();
	} else if(nav.canGoBack()){
		nav.pop();
	} else if(Date.now() - this.lastBack > 500) {
		this.platform.exitApp();
	}
	this.lastBack = Date.now();
});

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

platform.registerBackButtonAction(() => {
	const overlay = this.app._appRoot._overlayPortal.getActive();
	const nav = this.app.getActiveNav();
	const closeDelay = 2000;
	const spamDelay = 500;

	if(overlay && overlay.dismiss) {
		overlay.dismiss();
	} else if(nav.canGoBack()){
		nav.pop();
	} else if(Date.now() - this.lastBack > spamDelay && !this.allowClose) {
		this.allowClose = true;
		let toast = this.toastCtrl.create({
			message: this.translate.instant("general.close_toast"),
			duration: closeDelay,
			dismissOnPageChange: true
		});
		toast.onDidDismiss(() => {
			this.allowClose = false;
		});
		toast.present();
	} else if(Date.now() - this.lastBack < closeDelay && this.allowClose) {
		this.platform.exitApp();
	}
	this.lastBack = Date.now();
});

Not sure if there’s a neater way of writing that… lol

5 Likes