Ionic Pro app always quit on backbutton press on android


#1

In my ionic pro app, I have page stack but backbutton press just quit the app instead of going back. I have tried to register backbutton press but that is also not working. The app just quit on pressing back button. please provide some info regarding it. Thanks.

Following is out put of ionic info

@ionic/cli-utils  : 1.19.2
ionic (Ionic CLI) : 3.20.0

global packages:

cordova (Cordova CLI) : 8.0.0

local packages:

@ionic/app-scripts : 3.1.9
Cordova Platforms  : android 6.3.0
Ionic Framework    : ionic-angular 3.9.2

System:

Node : v6.9.0
npm  : 3.10.8
OS   : Windows 7

Environment Variables:

ANDROID_HOME : not set

Misc:

backend : pro

#2

What do you mean by this. What have you tried? With some concrete info like the code you attempted, we will be able to provide higher quality assistance.

Also, there are threads that have working examples, such as

ionViewWillEnter() {
this.handleBackButton = this.platform.registerBackButtonAction(() => {
logout(); 
},1);
}

That, plus a little googling will give you the answer you’re looking for. You just have to do some searching before simply asking for someone to provide the code for you.


#3

#4

Hi Thanks for the reply

I have tried the following code on app.component.ts
it is not catching the backbutton press. also when pressing backbutton, as expected behavior pages are not going back, instead application just quits. I have implemented backbutton tracing in past using ionic2 and they work fine , but here it seems little odd.

I have been trying on this for two days and googling as nothing helped , asking for help from experts here.

initializeApp() {
this.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.
this.statusBar.styleDefault();
this.platform.registerBackButtonAction(()=>{
console.log(‘back button action registered’);
let nav = this.app.getActiveNav();
if (nav.canGoBack()){ //Can we go back?
console.log(‘can go back’);

    }else{
      console.log('cannot go bakc');
      
    }
  });
});

}

is there something that I am doing wrong ?


#5

Yeah, you have to assign the callback to a function as opposed to just writing the function out. That’s what

Is suggesting.

If I get a chance tomorrow I’ll post my version of it. Its fully functional. I admit the process is more complex than one would expect.

I see there are suggestions that involve getting activeNav’s, etc… which may work but (no offense) turn into linguini code. There are much cleaner ways to do it


#6

Hi Thanks for looking into it. I will try this in view will appear as the same code you provided. but I doubt this could be some other issue too as every time backbutton press exit the app instead of goback to previous pages. have you ever see some symptom like this ?


#7

I tried code with home.ts in my project, not working. I still doubt there is something else which is controlling back button press and quit application whenever I press it.


#8

i have also same issue

when i press hardware back button it just exiting app, no nav pop happening

please help me with this


#9
imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp, {
      navExitApp: false,
    }),

I have used above code to prevent app from exiting in app.module.ts. But unfortunately this also doesn’t help


#11

Thank you for the help, I have used code with a new application and everything works perfect. But when I use it with this particular application that I have done, same code is not working. Is there any clue ? any assumptions regarding what could be checked.