0

I am trying to disable hardware back button on android device when $ionicModal is open.

$ionicModal.fromTemplateUrl('templates/dashboard.html', {
        scope: $scope,
        hardwareBackButtonClose : false
       }).then(function(modal) {
         $scope.modal = modal;
         $scope.modal.show();
      }); 

I am using following $ionicPlatform.registerBackButtonAction method for handle back button in app.js

$ionicPlatform.registerBackButtonAction(
                            function(event) {
                             if(navigator.connection.type != Connection.NONE) { 

                                if ($ionicHistory.currentStateName() == "app.dashboard") {
                                    if (localStorage.getItem('token') !== null
                                            && localStorage.getItem('token') !== undefined
                                            && localStorage.getItem('token') !== '' 
                                            && $rootScope.userId !== undefined) {

                                        $rootScope.confirmExitPopup = $ionicPopup.show({
                                                templateUrl : 'Views/ConfirmationPopup.html',
                                                title : 'demo',
                                                cssClass: 'customfilter-popup',
                                         });

                                        $rootScope.onSuccessPopup = function(){
                                             ionic.Platform.exitApp();

                                             $rootScope.confirmExitPopup.close();
                                         }

                                         $rootScope.closePopup = function () {

                                      $rootScope.confirmExitPopup.close();
                                           };

                                       } else {
                                           $ionicHistory.goBack();
                                       }
                                     } else {

                                           if($ionicHistory.backView()){
                                               $ionicHistory.goBack();
                                           }
                                           else{
                                               $state.go("app.dashboard"); 

                                           }


                                     }
                             }
                             else{
                                 ionic.Platform.exitApp();
                             }
                }, 1000);

When my $ionicModal is open then I click the hardware back button $ionicModal is not close but state is changed so I want to disable hardware back button when my $ionicModal is open. I have already added hardwareBackButtonClose : false property in $ionicModal but its override by $ionicPlatform.registerBackButtonAction method.

how to disable it? Thank you.

Riddhi Mistry
  • 31
  • 1
  • 9
  • Have you tried this https://stackoverflow.com/questions/37296999/ionic-2-disabling-back-button-for-a-specific-view – Paresh Gami Dec 07 '18 at 04:21
  • I am using ionic v1. – Riddhi Mistry Dec 07 '18 at 04:23
  • 1
    oh my bad. Please check this one https://stackoverflow.com/questions/26548418/disable-hardware-back-button-in-ionic-application – Paresh Gami Dec 07 '18 at 04:29
  • I have already used this method in my app.js file as I mentioned above. The method is called everytime so I handled back button for everypage. So my question is when the $ionicModal is open and I click on Android device hardwareback button the state is change automatically (this time $ionicPlatform.registerBackButtonAction method called because I defined in app.js) but the modal is not closing. If I am writing any method in specific controller $ionicPlatform.registerBackButtonAction is override all the method. – Riddhi Mistry Dec 07 '18 at 04:44
  • You only register registerBackButtonAction method when your modal is shown and deregister when modal is close – Paresh Gami Dec 07 '18 at 06:45
  • Okay but after what can I do handle back button for all pages ? If I registeredBackbuttonAction method only single time when modal is open then it doesn't work for all the pages backbutton that's why I registred back button in app.js so its automtically work for all the pages If modal is open or not. I want to handle all the pages back button and also handle $ionicModal. Can you describe more? – Riddhi Mistry Dec 07 '18 at 07:03

0 Answers0