3

I want to know which function is called when we click ion-navbar back button by default in ionic 3. I want to call the same function on hardware back button click.

1 Answer 1

6

You can use registerBackButtonAction of Platform Service. You can override hardware back button action as below inside app.component.ts. Remember to call registerBackButtonAction after Platform.ready().

import { Platform, App } from 'ionic-angular';

@Component({
  templateUrl: 'app.html'

})
export class MyApp {

  constructor(public platform: Platform, private app: App) {

    this.platform.ready().then(() => {

      this.platform.registerBackButtonAction(() => {

          let nav = this.app.getActiveNav()

          if (nav.canGoBack()) {
            // If there are pages in navigation stack go one page back
            // You can change this according to your requirement
            nav.pop();

          } else {

            // If there are no pages in navigation stack you can show a message to app user
            console.log("You cannot go back");
            // Or else you can exit from the app
            this.platform.exitApp();
          }
      });
    });
  }
}

Hope this will help you.

1
  • I was calling registerBackButtonAction() from each page rather than app.component.ts. Thanks for your assistance. Mar 11, 2019 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.