Not able to disable back press in ionic


#1

I am not able to disable the back press button.
I am using the the code" onHardwareBackButton(callback) ".
In callback function i am getting the alert successfully, but the back press will close the current page.
Please help me, how can i disable the default functionality of the back press.


#2

I’m assuming you’re trying to disable the Android back button since you’re referencing a hardware back button. This is not an ionic topic, but at best a phonegap topic. Regardless, this should do it for you.

document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
  e.preventDefault();
}

#3

Thanks for your help but the problem is still not solved.
On back press “onBackKeyDown(e)” is called but still it exits the app.


#4

Try this code.
I use it on various apps, and works as intented.

	//=================================================
	if(!$rootScope.$viewHistory.backView){
		$scope.backButton = $ionicPlatform.registerBackButtonAction( function () {
			$ionicViewService.nextViewOptions({ disableBack: true });
			$state.go('home');
		}, 105 );
		$scope.$on('$destroy', $scope.backButton);
	}
})

If the app has a view back, it does the back page event, else (to prevent exiting the app), redirects to home (there yo ucan just do a return false.
The 105 stands for the priority of the events. There are various ionic events as close side menu, exit modal, etc. The 100 stands for back page or exit app.


#5

still not working please reply