Barcode scanner closes page on cancel


I have a problem when using the plugin barcodescanner. When cancelling the opened barcode scanner, it closes the barcode scanner view, but the page where the Barcode.scan() function is thrown is also closed.

This is the code I’m trying:

if ('cordova')) {
            BarcodeScanner.scan().then(result => {
                if (!result.cancelled) {
                   .. do something
                } else {
                    console.log('Barcode scanner is cancelled')
            }, error => {

Can someone help me out with this issue?


I’m interested in this too. Did you find a solution?

I’m guessing no error is printed on your console, right?

I just found out the answer the question.

BarcodeScanner.scan ().then (success => {
  if (success.cancelled == 1) {
    //handle here whatever you want to do.

Thanks for the reply.

This still doesn’t solve the issue that when the success.cancelled == 1 is true, the original page is closed.

What I do is to have an independent page for the scanner. So when I handle the cancel thing, I have a this.navCtrl.pop (); and, of course, if you succeed with the scan, you load another page.

Use TRUE instead of 1 because ‘cancelled’ is a BOOLEAN :slight_smile:

But if you set that value to true, it returns an error.

This works for me, I added an messaje that the scanner was cancelled only with an alert validating the field result.text like this:

function fileViewSuccess(result) {
console.log("We got a barcode Result: " + result.text);
if(result.cancelled == true){
alert(“Was cancelled”);
///do something…


1 Like

Why dont you guys use ionViewCanLeave as solution ?

On opening barcode scanner just pop all observers from backbutton observers array.
async scan(){
var observers_container= this.platform.backButton.observers; //observers are removed temporarily
this.platform.backButton.observers= //empty array;
//here backbutton event only triggered in barcode scanner and not in webview.
. . .
. . .

You are interfering with backbutton. Removing observers will help prevent going to previous page. Just follow this…

var observers=this.platform.backButton.observers;
this.platform.backButton.observers=; //empty array
setTimeout(()=>{this.platform.backButton.observers=observers},500); // important to provide some time to close barcode