Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic registerBackButtonAction does not work

Here are the steps to reproduce:

  1. Create a basic ionic project ionic start test sidemenu
  2. Add the android platform ionic platform add android
  3. In app.js add the code:

     $ionicPlatform.registerBackButtonAction(function (event) { 
        alert("back button action handler");            
        event.preventDefault(); 
     }, 999);
    

    This code can be added in the .run method or in the $ionicPlatform.ready() method - same result, not working

  4. ionic build android then ionic upload -> or manually put the APK on a device

[BUG] - the alert is not shown and history view navigation is performed. It's like this action that I try to register is not taken into consideration.

What am I doing wrong? I tried this code in a controller also, also e.stopPropagation() or e.stopImmediatePropagation still no success.

I have the latest Ionic (1.4.5) and Cordova 4.3.0, tested on some Samsung devices. In Ripple, it works ok.

like image 998
gmodrogan Avatar asked May 27 '15 19:05

gmodrogan


Video Answer


1 Answers

try preventing the default first

$ionicPlatform.registerBackButtonAction(function (event) { 
    event.preventDefault();
    alert("back button action handler");  
 }, 999);

Also the code 999 is perfectly valid, the codes 100, 200, 300, 400, 500 are just the priorities that ionic asigns to certain actions of the back button. I've used multiple times the priority 900, it just puts your backbutton on top of all other action.

More information in the documentation : http://ionicframework.com/docs/api/service/$ionicPlatform/

like image 134
R.Villanueva Avatar answered Sep 28 '22 12:09

R.Villanueva