Hardware back button exiting app, even after overriding the registerBackButtonAction event

I am using below code for overriding back button:

var lastTimeBackPress = 0;
   var timePeriodToExit  = 2000;

this.platform.registerBackButtonAction((fn, priority) => {
        // get current active page
		priority=100;
		console.log('Enter');
        let view = this.nav.getActive();
		//var response=this.menuController.isOpen();
					console.log('Enter');

		if(!this.menuController.isOpen()){
						console.log('Enter1');

        if (view.component == HomePage) {
						console.log('Enter2');

            //Double check to exit app
            if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
                this.platform.exitApp(); //Exit from app
            } else {
							console.log('Enter3');

                let toast = this.toastCtrl.create({
                    message:  'Press back again to exit App',
                    duration: 2000,
                    position: 'bottom'
                });
                toast.present();
                lastTimeBackPress = new Date().getTime();
            }
        } else {
						console.log('Enter4');

            // go to previous page
            this.nav.setRoot(this.pages[0].component);
        }
					console.log('Enter5');

}{
				console.log('Enter6');

	this.menuController.close();
}
    });

Code was perfectly working before I updated the latest ionic version using below commands
npm install -g ionic@latest
npm install --save-dev --save-exact @ionic/cli-plugin-cordova@latest
npm install --save-dev --save-exact @ionic/cli-plugin-ionic-angular@latest

And adding multiple maps using below command
cordova plugin add https://github.com/mapsplugin/cordova-plugin-googlemaps-sdk --nofetch
cordova plugin add https://github.com/mapsplugin/cordova-plugin-googlemaps#multiple_maps \ --variable API_KEY_FOR_ANDROID=“MyKey” \ --nofetch

current Ionic version 3.7.0

Config.xml details(Removed start tag < from below, as it was not visible with start tag when I saved)

engine name=“android” spec="^6.2.3" />
engine name=“browser” spec="^4.1.0" />
plugin name=“com.googlemaps.ios” spec=“https://github.com/mapsplugin/cordova-plugin-googlemaps-sdk” />
plugin name=“cordova-plugin-admobpro” spec="^2.29.0" />
plugin name=“cordova-plugin-apprate” spec="^1.3.0" />
plugin name=“cordova-plugin-console” spec=“1.0.5” />
plugin name=“cordova-plugin-device” spec=“1.1.4” />
plugin name=“cordova-plugin-geolocation” spec="^2.4.3" />
plugin name=“cordova-plugin-googlemaps” spec=“https://github.com/mapsplugin/cordova-plugin-googlemaps#multiple_maps”>
variable name=“API_KEY_FOR_ANDROID” value=“MyKey” />
/plugin>
plugin name=“cordova-plugin-inappbrowser” spec="^1.7.1" />
plugin name=“cordova-plugin-network-information” spec="^1.3.3" />
plugin name=“cordova-plugin-splashscreen” spec="~4.0.1" />
plugin name=“cordova-plugin-statusbar” spec=“2.2.2” />
plugin name=“cordova-plugin-whitelist” spec=“1.3.1” />
plugin name=“cordova-plugin-x-socialsharing” spec="^5.1.8" />
plugin name=“cordova-sqlite-storage” spec="^2.0.4" />
plugin name=“ionic-plugin-keyboard” spec="~2.2.1" />
plugin name=“phonegap-plugin-push” spec="^1.10.5">
variable name=“SENDER_ID” value=“Id” />
/plugin>
Dependencies from pacakge.json

"dependencies": {
    "@angular/common": "4.1.2",
    "@angular/compiler": "4.1.2",
    "@angular/compiler-cli": "4.1.2",
    "@angular/core": "4.1.2",
    "@angular/forms": "4.1.2",
    "@angular/http": "4.1.2",
    "@angular/platform-browser": "4.1.2",
    "@angular/platform-browser-dynamic": "4.1.2",
    "@ionic-native/admob": "^3.12.1",
    "@ionic-native/app-rate": "^3.12.1",
    "@ionic-native/core": "3.10.2",
    "@ionic-native/geolocation": "^4.1.0",
    "@ionic-native/google-maps": "^4.1.0",
    "@ionic-native/in-app-browser": "^3.14.0",
    "@ionic-native/network": "^3.12.1",
    "@ionic-native/push": "^3.12.1",
    "@ionic-native/social-sharing": "^3.14.0",
    "@ionic-native/splash-screen": "3.10.2",
    "@ionic-native/status-bar": "3.10.2",
    "@ionic/cloud-angular": "^0.12.0",
    "@ionic/storage": "^2.0.1",
    "cordova-android": "^6.2.3",
    "cordova-browser": "^4.1.0",
    "cordova-plugin-admobpro": "^2.29.0",
    "cordova-plugin-apprate": "^1.3.0",
    "cordova-plugin-compat": "^1.0.0",
    "cordova-plugin-console": "1.0.5",
    "cordova-plugin-device": "1.1.4",
    "cordova-plugin-dialogs": "^1.3.3",
    "cordova-plugin-extension": "^1.5.1",
    "cordova-plugin-geolocation": "^2.4.3",
    "cordova-plugin-globalization": "^1.0.7",
    "cordova-plugin-inappbrowser": "^1.7.1",
    "cordova-plugin-network-information": "^1.3.3",
    "cordova-plugin-splashscreen": "~4.0.1",
    "cordova-plugin-statusbar": "2.2.2",
    "cordova-plugin-whitelist": "1.3.1",
    "cordova-plugin-x-socialsharing": "^5.1.8",
    "cordova-sqlite-storage": "^2.0.4",
    "es6-promise-plugin": "^4.1.0",
    "ionic-angular": "3.3.0",
    "ionic-native": "^2.9.0",
    "ionic-plugin-keyboard": "~2.2.1",
    "ionicons": "3.0.0",
    "phonegap-plugin-push": "^1.10.5",
    "rxjs": "5.1.1",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.11",
    "com.googlemaps.ios": "https://github.com/mapsplugin/cordova-plugin-googlemaps-sdk",
    "cordova-plugin-googlemaps": "https://github.com/mapsplugin/cordova-plugin-googlemaps#multiple_maps"
},
"devDependencies": {
    "@ionic/app-scripts": "1.3.7",
    "@ionic/cli-plugin-cordova": "1.6.2",
    "@ionic/cli-plugin-ionic-angular": "1.4.1",
    "ionic": "3.7.0",
    "typescript": "2.3.3"
},
"cordovaPlugins": [
    "cordova-plugin-whitelist",
    "cordova-plugin-console",
    "cordova-plugin-statusbar",
    "cordova-plugin-device",
    "cordova-plugin-splashscreen",
    "ionic-plugin-keyboard"
],

Not Completly sure Can the issue be because of below fix done for maps

Jul/13/2017
(JS) Bug fix: Map is not displayed when you go back from another page using the back button of Android. In order to resolve this issue, the plugin catches the backbutton event all the time. But other plugins or your JS code might want to catch the backbutton event. In order to passes the event, the maps plugin fires the plugin_backbutton event alternatively. (But this is for just in case)

Please help to suggest fix for this issue

1 Like

I have the same problem. Hardware back button exiting app - doesn’t matter how “deep” I am in application.

When I uninstalled https://github.com/mapsplugin/cordova-plugin-googlemaps#multiple_maps everything back to normal. Hardware back button worked as should, but now I don’t have a maps ;(

Anyone found how to fix it?

ionic info

@ionic/cli-plugin-cordova       : 1.6.2
@ionic/cli-plugin-ionic-angular : 1.4.1
@ionic/cli-utils                : 1.7.0
ionic (Ionic CLI)               : 3.7.0

global packages:

Cordova CLI : 7.0.1

local packages:

@ionic/app-scripts : 2.0.2
Cordova Platforms  : android 6.2.3 ios 4.4.0
Ionic Framework    : ionic-angular 3.6.0

System:

Android SDK Tools : 25.2.5
Node              : v6.11.1
OS                : macOS Sierra
Xcode             : Xcode 8.3.3 Build version 8E3004b
ios-deploy        : 1.9.1
ios-sim           : 5.0.13
npm               : 3.10.10

I found solution:

downgrade googlemaps plugin to version: 1.4

ionic cordova plugin add cordova-plugin-googlemaps --variable …

but

Ionic docs have:
ionic cordova plugin add https://github.com/mapsplugin/cordova-plugin-googlemaps#multiple_maps… <- this is beta version

3 Likes

Can confirm that this solution works. Thank you so much sir, this was causing me a big headache.

You saved my day buddy :*

I had the same problem and I recently posted a question about it. Strange the search had not returned this thread. After 2 weeks of hair pulling I also found that the google maps plugin v2 was the culprit. It is in beta right now and I had a buggy version. The v2 beta is updated all the time. After re-installing the plugin with the latest, the problem was solved. Cordova can be such a time waster sometimes !

Thanks alot for this solution! Saved me so much time!