Skip to content Skip to sidebar Skip to footer

Angularjs Device Back Button Not Working

I am trying to create dialog when device back button clicked, searched internet and came up with the coding. 1.when i use the below coding, Clicking back button shows dialog ok and

Solution 1:

sry, I have changed the code like this then also the same the app closes for both true and false of confirm..

app.run(['$rootScope','$mdDialog','$cordovaDialogs', function($rootScope, $mdDialog, $cordovaDialogs) {
  document.addEventListener("deviceready", function() {
    console.log("deviceready");
    document.addEventListener("backbutton", onBackKeyDown,false);
      function onBackKeyDown() {
        if(confirm("Are You sure You wanna Exit?")){
          console.log("true");
          navigator.app.exitApp();
        }else{
          return false;
     }
    }
  },true);
  $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
    $rootScope.title = current.$$route.title;
  });
}]);

Solution 2:

Thanks for the support guys, I have completed this issue by getting help from my mates.. Please see the code below.. :)

app.run(['$rootScope','$location', function($rootScope,$location) {
  document.addEventListener("deviceready", function() {
    console.log("deviceready");
    document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
  e.preventDefault();
  if ($location.path() === "/login" || $location.path() === "/home") {
  var r=confirm("exit");
	if(r==true){
		console.log("not exit");
		navigator.app.exitApp();
	}else {
     navigator.app.goBack();
    }
}else {
    /* $ionicHistory.goBack(); */
	window.history.back();
    navigator.app.goBack();
}
}
}, 100);
$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
    $rootScope.title = current.$$route.title;
  });
}]);

Post a Comment for "Angularjs Device Back Button Not Working"