Handling hardware back button with barCode scanner plugin


#1

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,

app.js

  .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

    $scope.scanBarcode=function(){
   $cordovaBarcodeScanner
      .scan()
      .then(function(barcodeData) {
            // Success! Barcode data is here
             console.log(barcodeData);//cancelled,text,format
               if(!barcodeData.cancelled){//got the data
                    $scope.search={
                         code:barcodeData.text
                     };
                }else{ //cancelled by user
                          //handel backbutton
              $ionicPlatform.onHardwareBackButton(function(event) {
                         event.preventDefault();
                        event.stopPropagation();
                        console.log("going back now y");
                 });
              }//else
     }, function(error) {//fail callback
        // An error occurred with scanner
        alert(error);
      });
}

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


#2

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) {
             event.preventDefault();
            event.stopPropagation();
            console.log("going back now y");
     });
  }

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


#3

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/


#4

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


#5

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