Handling hardware back button with barCode scanner plugin


I am coding for android with ionic cli 1.4.3
i have an app in which i navigate to the scanView where the i have a button which scans for barcode this is the plugin https://github.com/wildabeast/BarcodeScanner

It works fine when the scan is success full, but if the user hits the back button without scanning the scanner window closes and the view is also navigated to the back view.

Only the scan window should close and the view should not change, that is the kind of behavior i am looking for.

I am using the Tabs template here is some of my code,


  .state('tab', {
url: '/tab',
    abstract: true,
    templateUrl: 'templates/tabs.html'
    .state('tab.home', {
    url: '/home',
    views: {
      'tab-home': {
        templateUrl: 'templates/tab-home.html',
        controller: 'homeCtrl'
.state('tab.scanView ', {
      url: '/scanView',
       views: {
         'tab-home': {
          templateUrl: 'templates/home-scanView.html',
          controller: 'scanViewCtrl'

in my controller i have this

      .then(function(barcodeData) {
            // Success! Barcode data is here
               if(!barcodeData.cancelled){//got the data
                }else{ //cancelled by user
                          //handel backbutton
              $ionicPlatform.onHardwareBackButton(function(event) {
                        console.log("going back now y");
     }, function(error) {//fail callback
        // An error occurred with scanner

So when the scan is cancelled by the user using the back button the view also changes.
How to i prevent that.


Read this article: http://www.gajotres.net/ionic-framework-handling-android-back-button-like-a-pro/

Essentially, you need to do this:

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

Instead of

  $ionicPlatform.onHardwareBackButton(function(event) {
            console.log("going back now y");

Everything is described in an attached article. Feel free to ask if you need more information.


I am already doing the registerBackAction on my view…i am detecting the state and navigating to it.

Can i do multiple actions with the backbutton?
For example when the when the barcode scanner is open one tap on the back button i want to close the app on the second tap i want to navigate to the previous view.

The issue can be seen here https://github.com/wildabeast/BarcodeScanner/issues/97

Also i found a work around(hack) here http://marsbomber.com/2014/05/29/BarcodeScanner-With-Ionic/


That’s why you need to mention this information, in your example you’re using onHardwareBackButton function.


yeah my bad, but when the backbutton is pressed it is pressed in the barcode scanner plugin so it seems like a plugin issue