Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable hardware back button in Ionic application?

I'm trying to disable the back button on my Cordova app. I'm using AngularJS + Ionic Framework. I found topics about this and tried the code bellow, but it has absolutely no effect. Any idea?

index.html

<head>     <script>       document.addEventListener("deviceready", onDeviceReady, false);         function onDeviceReady() {             document.addEventListener("backbutton", function (e) {                 e.preventDefault();                 console.log("hello");             }, false );         }     </script> </head> 

Note that when I push back button, I have "hello" displayed in my console.

like image 363
David D. Avatar asked Oct 24 '14 13:10

David D.


People also ask

How do you handle hardware back button in Ionic?

The hardware back button is found on most Android devices. In native applications it can be used to close modals, navigate to the previous view, exit an app, and more. By default in Ionic, when the back button is pressed, the current view will be popped off the navigation stack, and the previous view will be displayed.

How do you close an app on back pressed in Ionic?

backButton. subscribeWithPriority(99999, () => { navigator['app']. exitApp(); });

How do you handle the back button in Ionic 3?

In Android application we generally press/ tap back to go back view or page but in root activity or root page in Ionic application this back press operation closes or minimize the application to the recent list.

How do I exit an Ionic app?

App. exitApp(); Force exit the app. This should only be used in conjunction with the backButton handler for Android to exit the app when navigation is complete.


2 Answers

Finally found the answer on this Ionic Forum thread:

$ionicPlatform.registerBackButtonAction(function () {   if (condition) {     navigator.app.exitApp();   } else {     handle back action!   } }, 100); 

$ionicPlatform.registerBackButtonAction allows to completly overwrite back button behavior. First param is a callback function and the secondone a priority (only the callback with the highest priority gets executed).

like image 64
David D. Avatar answered Sep 29 '22 16:09

David D.


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

this will prevent back button functionality.

like image 26
Muhammad Faizan Khan Avatar answered Sep 29 '22 15:09

Muhammad Faizan Khan