How to catch a url between Android Intents

0

I have a native android app with 2 Activities.

1.- Main.java: contains a webview where a web POS is displayed with url: www.posWeb.com

2.- Camera.java: this activity sends a barcode reader to scan the products that are going to be searched in inventory or added to the cart.

The idea that I have is to send the bar code read from Camera.java to the Main, via the following code

    Intent i = new Intent(getApplicationContext(),Main.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra("codeBar",rawResult.getText());
    startActivity(i);
    finish();

But the problem I have is that I need the webview to load a url depending on whether the application was started or if it came back from Camera.java

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
        }
    });
     webView.getSettings().setJavaScriptEnabled(true);
     webView.getSettings().setDomStorageEnabled(true);
     webView.setVerticalScrollBarEnabled(false);
     webView.setHorizontalScrollBarEnabled(false);

Currently I am handling it in the following way:

             if(getIntent().hasExtra("codeBar")) //el extra que envie desde Camera.java
              webView.loadUrl(url+getIntent().getStringExtra("codeBar"));
             else
               webView.loadUrl(url);

Any suggestions?

    
asked by Rodrigo Jimenez 18.08.2017 в 03:04
source

1 answer

1

Use startActivityForResult . This enables the activity that is running to pass data for the activity that executes it:

In MainActivity you would call Camara activity like this:

Intent intent = new Intent(this, Camera.class);
startActivityForResult(intent, 100);

When you get the QR in Camera , you end the activity assigned the QR data with setResult by passing the Intent with the data:

Intent data = new Intent();
data.putExtra("qr",rawResult.getText());
setResult(Activity.RESULT_OK, data);   
finish();

When the activity camera is finished, setResult sent it data to MainActivity and to receive it we overwrote the method onActivityResult :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 100) {
        if(resultCode == Activity.RESULT_OK){
            String dataQR = data.getStringExtra("qr");
            webView.loadUrl(url+dataQR);
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            // Codigo aqui si la respuesta fue cancelada
        }
    }
}
    
answered by 18.08.2017 / 03:45
source