I have an Ionic 2.2 project where I override the default behavior of the back button using platform.registerBackButtonAction(). But something strange happened because it used to work but now the callback never gets called and the app just exits. I do the following in the app.components.ts file:
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { TabsPage } from '../pages/tabs/tabs';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage = TabsPage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
platform.registerBackButtonAction(() => {
console.log("Backbutton pressed.");
}, 100);
});
}
}
The same code works in a vanilla starter app. I have debugged the register function and I see that my callback is added to the Platform backbutton callbacks array normally.
I also tried with different priority levels with no luck.
This is so annoying, I spent days trying to figure out what is wrong. I did the usual re-install of the node modules and platform etc. I also tried going back to cordova 6, but it is not that.
Anyone has a suggestion ? I’m really pulling my hair out on this one. Am I the only one that problem ever happened?
I also tried this without any luck:
document.addEventListener("backbutton", function () {
}, false);
It seems the ionic or cordova back button handler mechanism is completely ignored. The application closes instantly. I even debugged the ionic code where the back button handlers are called and my breakpoints never get triggered. In fact I see it enter the function in a flash but something closes the application and so it does not have time to stop on the breakpoint.
I don’t know how much sense it make to give advice when your app-scripts is so out of date. You could be running into all kinds of bugs. Unless there’s a reason you just can’t update, I’d recommend you update everything.
Yes I understand and that’s what I am considering now. Up to now my ionic 2 project was working fine and I did not want to upgrade to ionic 3 fearing that things would not work well anymore. But now, I guess I have no other choice.
Effectively!, I use native google maps and native geolocation., I will try encapsulate geolocation inside function and call it before or later to test it.
Ok, and are you using the v2 beta google maps plugin by any chance ? I am and I think that when I re-installed my plugins to solve another issue, it upgraded the google maps plugin to latest version and there may be a problem with the latest beta. This would make sense cause the problem really appeared out of nowhere.
I am in the process of upgrading to ionic 3 right now but if I still have that back button issue after the ionic upgrade, I will try reverting to an older google maps plugin version, maybe v1 instead of v2 beta.
However, the latest google maps plugin v2 beta has other issues. It really slows down the whole app, not just the page with the map. My lists are super slow now, it takes 2 seconds to react to a tap on a button, everything is sluggish. So I uninstalled that plugin. Will try with an older version instead.