How to sync ionic navigation bar and hardware button in android in a phonegap mobile app

In my phonegap app, there is a ionic navigation bar which has the back button. If we navigate the app using this navigation bar it will navigate properly to each page, but if we use the hardware back button in some point of time, navigation will get messed up. Is there any fixes for this.

<ion-view view-title="Store Locator" ng-controller="storelistCtrl" >
    <ion-nav-buttons side="right">
         <button form="searchId" class="button button-icon icon ion-ios7-search" ng-click="search(searchForm.searchText)"></button>
    </ion-nav-buttons>
    <ion-content>
        some code here....
    </ion-content>  
</ion-view> 

This is a page in our app. for the back button i have overrided the actions with ioPlatform.registerBackButtonAction

$ionicPlatform.registerBackButtonAction(function (event) {
       if($state.current.name==="app.home"){

             var myPopup = $ionicPopup.show({
                    title: 'Exit the application',
                    scope: $scope,
                    buttons: [
                      { text: 'Cancel', 
                        onTap: function(e) {
                            $state.go("app.home");
                        }
                      },
                      {
                        text: 'Yes',
                        type: 'button-positive',
                        onTap: function(e) {
                            navigator.app.exitApp();
                        }
                      }
                    ]
                  });
        }
        else if($state.current.name==="app.couponlists"){
            $state.go('app.home');
        }
        else if($state.current.name==="app.deallists"){
            $state.go('app.home');
        }
        else if($state.current.name==="app.coupondetail"){
            $state.go('app.couponlists');
        }
        else{
          navigator.app.backHistory();
        }
      }, 100);

Go through the Docs first before implementing. Ionic Platform Doc

Options available are

  1. onHardwareBackButton(callback)
  2. offHardwareBackButton(callback)
  3. registerBackButtonAction(callback, priority, [actionId])
  4. on(type, callback)

make sure your $ionicPlatform.ready() is called first.

You need to use $ionicPLatform.registerBackButtonAction to do it, redirect according to states, but you need to make sure that $ionicPlatform.ready() is called first, see below code

$ionicPlatform.registerBackButtonAction(function (event) {
      if ($state.$current.name=="app.login" || $state.$current.name=="app.signup"){
              // Do not go to the previous state (or view) for these states. 
              // Do nothing here to disable H/W back button.
              $cordovaDialogs.alert('Going back is not allowed for this page', 'Notice', 'OK')
              .then(function() {
                // callback success
              });
          }
          else if($state.$current.name=="app.productlist")
          {
            $location.path("/productlist");
          } else {
              // For all other states, the H/W BACK button is enabled
              navigator.app.backHistory();
          }  }, 100);