Hardware back button exit the app in mobile device?

I have 25 pages in my project

so I want to handle hardware back button

I wrote hardware back button code in my app.component.ts file but it doesn’t work

this.platform.registerBackButtonAction(() => 
      {
        let nav = this._app.getActiveNavs()[0];
        let activeView = nav.getActive();

        if(activeView.name == "Demo1Page" || activeView.name == "Demo2Page") 
        {
          nav.pop();
        }
        else if(activeView.name == "Demo3Page" || activeView.name == "Demo4Page") 
        {
          nav.setRoot('TabsPage');
        }
        else if(activeView.name == "HomePage") 
        {
          this.platform.exitApp();
        }
      });

i don’t know what you exactly want but in my app this code work perfactly with linear navigation…

add this in constructor

private app: App

 platform.registerBackButtonAction(() => {
        const overlayView = this.app._appRoot._overlayPortal._views[0];
        if (overlayView && overlayView.dismiss) {
          overlayView.dismiss();
          return;
        }
        if (this.nav.canGoBack()) {
          this.nav.pop();
        }
        else {
          let view = this.nav.getActive();
          if (view.component == DashboardPage || view.component == LoginPage) {
            if (this.alert) {
              this.alert.dismiss();
              this.alert = null;
            } else {
              this.platform.exitApp();
              //this.showAlert();
            }
          }
        }
      });
1 Like

Hello,
@hirenkorat3

thanks for your replied

When I push one page it will pop by hardware back button

but when I setRoot page
then it will not close the app(Not working hardware back button on setRoot Page)

When I alert(view.component.name) i got only “t”

how I will get the page name?

Please help me

this is the code you want .here i have to exit from the page if it’s loginpage or dashboard page otherwise it will remove all page one by one from navcontroller.

let view = this.nav.getActive();
if (view.component == DashboardPage || view.component == LoginPage) {
     if (this.alert) {
           this.alert.dismiss();
           this.alert = null;
     } else {
           this.platform.exitApp();
           this.showAlert();
     }
}
1 Like

Hello,
@hirenkorat3

when i alert(view.component)
i got “t”

and it didn’t go to
if (view.component == DashboardPage || view.component == LoginPage)
{ }
part

But after write

let view = this.nav.getActive().id;

I got Current Page name

now, my problem gets resolved

Thank you @hirenkorat3

for supporting me

1 Like

Hi, Below code I’ve written in my app.component.cs
platform.ready().then(() => {
// this.firebase.grantPermission();
statusBar.styleDefault();
let splash = modalCtrl.create(SplashPage);
splashScreen.hide();
splash.present();
platform.registerBackButtonAction(() => {
const overlayView = this.app._appRoot._overlayPortal._views[0];
if (overlayView && overlayView.dismiss) {
overlayView.dismiss();
return;
}
if (this.nav.canGoBack()) {
this.nav.pop();
}
else {
let view = this.nav.getActive();
if (view.component == “HomePage” || view.component ==“TabsPage”|| view.component ==“SplashPage”) {
this.platform.exitApp();
//this.showAlert();
}
}
});
});

There are multiple screen in my app. My goal is when user is on home page and click on device back button it should ask “are you sure want to close this app?” … because of above code back button is not working… please help me