Handling the hardware back buttons

Awesome! Working fine. Exactly I wanted this.

.controller(‘AppCtrl’, function( …,$ionicPlatform) {
$ionicPlatform.ready(function() {

$ionicPlatform.registerBackButtonAction(function(event){
event.preventDefault();
alert(‘nope’);
}, 100);

$ionicPlatform.on(‘resume’, function(){

})
})
})

nothing happens still returning. :confused:

i’m using too (for another things)

$ionicPlatform.on(‘resume’, function(){

and works fine

Continuing the discussion from Handling the hardware back buttons:

Problem solved.
When u use an modal and u dont want the (android)back button close it, u need to increase the priority.

for example:

$ionicPlatform.registerBackButtonAction(function(event){}, 100);
to
$ionicPlatform.registerBackButtonAction(function(event){}, 1000);

1 Like

Hi @bengtler, What do you use to catch the event? I’m using ng-idle to have the user login again if inactivity is too long. ng-idle has a method to register event names that reset the inactivity clock count. I want to pass the back button event name to this method.

i would not use a external module for that.

I would add a directive to the body of my app. this directive starts an interval and listens to events like touchstart, click, … maybe some more and resets the interval if one event is triggered. If the interval function is executed --> the user gets logged out or you can trigger also a angularjs event. So you can set your interval to 10 minutes --> if the user scrolls, taps, touches around --> reset the interval --> if he is doing nothing 10 minutes $broadcast the event on the $rootScope.

Hi,

I agree that $ionicPlatform.registerBackButtonAction does not work when running through the Ionic View app, although it works perfectly when the app is installed with an APK.

Is there any way to deactivate the Android back button when running the app through Ionic View?

1 Like

Whicj js file did you place this code in?

you can add it in app.js or you can call it though a controller. If your new then i would suggest you read through the doc’s and if you still face a problem ping us here with a code snippet through plunker

Absolutely excellent and worked best. I was using $ionicHistory.goBack() though it worked but could not return to previous multiple states visited. Thanks

Have you found a solution to have it working on ionic view?

We are facing the same problem: it works fine with the APK but fails on ionic view.

hey @franco4 if my app use this $ionicConfigProvider.views.maxCache(0)…
$ionicHistory.goBack() no work :confused:
but i just use ionic.Platform.backHistory(); to back

In my case it worked but after changing 100 to 101 according to official documents of IONIC

The priorities for the existing back button hooks are as follows: Return to previous view = 100 Close side menu = 150 Dismiss modal = 200 Close action sheet = 300 Dismiss popup = 400 Dismiss loading overlay = 500

Your back button action will override each of the above actions whose priority is less than the priority you provide. For example, an action assigned a priority of 101 will override the ‘return to previous view’ action, but not any of the other actions.

http://ionicframework.com/docs/api/service/$ionicPlatform/

// Disable BACK button on home
$ionicPlatform.registerBackButtonAction(function (event) {
if($state.current.name==“app.home”){
navigator.app.exitApp();
}
else {
navigator.app.backHistory();
}
}, 100);

@ThomasDalla Thank you so much for the help! It worked for me! :smiley:

i have same issue but its not worked with me can you please give me someone sample project

  //this code to override the default behavior of hardware back button
  $ionicPlatform.registerBackButtonAction(function (event) {
if($state.current.name=="app.main" || $state.current.name == "app.fakeLogin"){
  //Check is popup dialog is not open.
  if(jQuery('[id^=dialog]').length == 0 ) {

      // mdDialog for show $mdDialog to ask for
      // Confirmation to close the application.

      $mdDialog.show({
          controller: 'DialogController',
          templateUrl: 'confirm-dialog.html',
          targetEvent: null,
          locals: {
              displayOption: {
                  title: "Bildirim",
                  content: "Uygulamadan çıkmak ister misin ?",
                  ok: "Evet",
                  cancel: "Hayır"
              }
          }
      }).then(function () {
          //If user tap Confirm at the popup dialog.
          //Application will close.
          ionic.Platform.exitApp();
      }, function () {
          // For cancel button actions.
      }); //End mdDialog
  }
}
else {
  navigator.app.backHistory();
}
}, 101);

Ok thank You so much . Its Work now:slight_smile:

To prevent App from device back button functionality use,

      $ionicPlatform.registerBackButtonAction(function (event) {
           event.preventDefault();
      }, 100);

If you want to prevent the particular page use,

   $ionicPlatform.registerBackButtonAction(function (event) {
       event.preventDefault();
       if ($location.path() === "/pagename" || $location.path() === "pagename") {
         navigator.app.exitApp();
       } else {
         $ionicHistory.goBack();
       }
    }, 100);

thank you . working fine for my app…

thanks it’s work for me.