I am currently working with a POS WEB that includes barcode scanning. For this the POS is in WebView
, and when I click the button to read the barcode I code the url to send the activity
of the bar code reader, since this is native to Android.
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
view.setVisibility(View.GONE);
ibtnRefresh.setVisibility(View.VISIBLE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onLoadResource(WebView view, String url) {
showPB();
if(!url.contains("sendScanReader"))
{
}else if(!url.contains("Settings")) {
}else{
view.loadUrl(Common.getURL());
}
}
@Override
public void onPageFinished(WebView view, String url) {
//Toast.makeText(getApplicationContext(),"Carga Finalizada",Toast.LENGTH_LONG).show();
hidePB();
if(url.contains("sendScanReader"))
{
hidePB();
Intent i = new Intent(getApplicationContext(),Main2Activity.class);
startActivityForResult(i,100);
}else if(url.contains("vta")){...
The problem is that Main2Activity (which is the reading activity) apparently opens 2 times. So when I do the process for the handleResult, the reader "closes", but the app shows me again the activity.
This is the HandleResult of the activity reader
@Override
public void handleResult(Result rawResult) {
// Do something with the result here
Log.v("main", rawResult.getText()); // Prints scan results
Log.v("main", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
String newURL = Common.getURL()+"?"+rawResult.getText();
Common.setURL(newURL);
mScannerView.stopCamera(); // Stop camera on pause
mScannerView = null;
Intent i = new Intent(getApplicationContext(),Main.class);
setResult(Activity.RESULT_OK, i);
finish();
}
Here is the fragment of the manifest. I also try with singleInstance but it does not work for me.
<activity android:name=".Main2Activity"
android:launchMode="singleTask"/>
Any suggestions?
Edit:
I add the showPB () and hidePb () code to avoid possible confusion.
public void showPB(){
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
}
});
}
public void hidePB(){
runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(getApplicationContext(),"Funcion para ocultar PB",Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
});
}