Its sometime tricky to handle the default behavior mobile OS provide. Ionic provides quite a simple yet tricky way to hand the functionality. Without Much a do. Here is an code snippet to show how to handle the Hardware/Soft Backbutton functionality in Ionic/Cordova/Phonegap framework:

$ionicPlatform.registerBackButtonAction(function () {
  if(CONDITION){
      // DO YOUR FUNCTIONALITY HERE
  }else{	
      // OTHERWISE FALLBACK TO NORMAL FUNCTIONALITY OF GOING BACK
       $ionicHistory.goBack();
  }
}, 100);

We need to provide $ionicPlatform and $ionicHistory as dependency for this code to work. The change made through this piece of code would be working across the application.

Code Explanation: Code is in itself self explanatory as we are registering for BackButton using the $IonicPlatform service and if our condition matches we can add any behavior to our app otherwise $IonicHistory would make sure the normal back button functionality is working.




Categories: IonicMobile

Leave a Reply

Your email address will not be published. Required fields are marked *