How to prevent an Android Activity from being duplicated

0

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);
        }
    });
}
    
asked by Rodrigo Jimenez 20.09.2017 в 19:47
source

2 answers

2

I am resolved. What I did was just add flags to my Intent.

@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);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //esta es la linea nueva con la que evito los duplicados.
            startActivityForResult(i,100);
    
answered by 21.09.2017 / 20:55
source
0

In onPageFinished() you perform the Intent to open Main2Activity , remember that the onPageFinished() method is called upon completion of the page load within the WebView .

The problem you mention may arise if you load more than once a url within WebView .

Both shouldOverrideUrlLoading() as onLoadResource() are loading the url within WebView by:

view.loadUrl(...);
    
answered by 21.09.2017 в 01:04