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 (this.platform.is('cordova')) {
BarcodeScanner.scan().then(result => {
if (!result.cancelled) {
.. do something
} else {
console.log('Barcode scanner is cancelled')
}
}, error => {
console.log(error);
});
});
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.
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;
this.barcode.scan(options).then((result)=>{
//here backbutton event only triggered in barcode scanner and not in webview.
this.platform.backButton.observers=observers_container;
if(result.cancelled==true){
. . .
}
if(result.cancelled==false){
. . .
}
});
}
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
this.barcode.scan(“Scanning”).then((data)=>{
setTimeout(()=>{this.platform.backButton.observers=observers},500); // important to provide some time to close barcode
});