Connor Brady
Connor Brady

Reputation: 91

Ionic - Handling NavBar back button?

All the solutions for handling going back via the hardware button in Ionic have all worked using this:

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

However, I was wondering if there is a way to handle the back button that appears in the Navbar as this doesn't seem to be fired when I press that in Android. All solutions I've tried to find refer to the code at the top that only appears to work for hardware buttons only.

Upvotes: 2

Views: 3032

Answers (2)

Prashant
Prashant

Reputation: 1385

I have achieved it in ionic-3 with this code.

   import { Navbar } from 'ionic-angular';

   export class myCustomClass {

   @ViewChild(Navbar) navBar: Navbar;

   ...

   ionViewDidLoad() {
       this.setBackButtonAction()
   }

   //Method to override the default back button action
   setBackButtonAction(){
     this.navBar.backButtonClick = () => {
     //Write here wherever you wanna do
      this.navCtrl.pop()
     }
   }

Upvotes: 7

Sathyajith
Sathyajith

Reputation: 4034

There is one backButtonClick() function present in the ionic navbar, you can override it like something below to get the navbar back button click event

backButtonClick() {
 console.log('// dos omething')
}

ionViewDidEnter() {
 this.navBar.backButtonClick = this.backButtonClick;
}

Upvotes: 3

Related Questions