Back button of Android device does not work


#1

Good morning…

I added to my ionic v2 app an registerBackButtonAction() where :

if(page==x){ pop } else { setRoot(HomePage) }

When i do “ionic cordova run android” the app works fine on my android device, but when i do “ionic cordova build android --prod --release” and upload the app to play store the “setRoot” function does not work. Do you have any idea why this is happening??

Thanks…


#2

Noone has any idea guys??


#3

Could you display your code and not pseudo code?


#4

this.platform.registerBackButtonAction(() => {
if(this.menuCtrl.isOpen()){
this.menuCtrl.close();
} else {
let view = this.navCtrl.getActive();
if(view.component.name==“WelcomePage”){
this.platform.exitApp();
} else if(view.component.name==“HomePage”){
this.navCtrl.setRoot(WelcomePage);
} else {
this.navCtrl.pop();
}
}
});


#5

Thx.

So a couple of idea:

  • Does it work when you build your app ionic cordova build android --prod and test the APK on your phone respectively could you reproduce the same problem without having to deploy in store? If yes, then you could add some alert("something); to debug

  • If only this.navCtrl.setRoot(...) isn’t working maybe you could try to use this.app.getRootNav().setRoot(...

  • Do you unregister your action too? It seems not, I would

    1. save the registration state

    this.unregisterCustomBackActionFunction = this.platform.registerBackButtonAction(() => {…

    1. and then unsubscribe if necessary like for example

    ionViewDidLeave() {
    if (this.unregisterCustomBackActionFunction != null) {
    this.unregisterCustomBackActionFunction();
    }
    }

Hope that could help…don’t have any other idea


#6

Thank you, I will try these ways!


#7

any update from this
I had same problem too

registerBack on device ready

when use ionic cordova run android --device //work fine
but use ionic cordova build android --prod //not work


#8

Can you post your code?


#9

I solved problem
I use view.component.name to check active view
when debug mode its work fine

but when production mode view.component.name has change because compiler

then i use view.id to check active view

its back to work normally


#10

Great! I had problem with view.component.name too! So i keep a global variable with the name of the active view and all work fine!


#11

Cool to hear you found your solution :slight_smile: You could know mark this issue as solved :wink:


#12

please sen your code