Lisa Anne
Lisa Anne

Reputation: 4595

Android webview how to detect when reloads

Hi and thank for your attention!

Please I have a WebView in my android application.

I need to detect when the WebView reloads the URL.

To be clear: the webpage contained in my WebView reloads when the user presses a button on the web page, BUT ID DOES NOT CHANGE URL,

Therefore shouldOverrideUrlLoading does not get called.

How do I detect the URL reloading???


This is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.preappove);
    wait = new ProgressDialog(this);
    wait.setTitle("Please Wait");
    wait.setMessage("Redirecting to PayPal for your approval");
    wait.show();
    wv = (WebView) findViewById(R.id.webView);
    wv.clearCache(true);
    wv.clearHistory();
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    wv.getSettings().setLoadWithOverviewMode(true);
    wv.getSettings().setUseWideViewPort(true);
    perfs = getSharedPreferences("pefs",
            PreApprove.this.MODE_MULTI_PROCESS);
    sender = perfs.getString("sender", null);
    receiver = perfs.getString("receiver", null);
    paymentamount = perfs.getString("paymentamount", null);
    maxpaymentamount = perfs.getString("maxpaymentamount", null);
    paymentnumber = perfs.getString("paymentnumber", null);
    // wv.setWebViewClient(new Callback());
    wv.getSettings().setBuiltInZoomControls(true);
    GetPreappovalKey gpk = new GetPreappovalKey();
    try {
        gpk.execute().get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    wv.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            wait.dismiss();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("www.google.com")) {
                Welcome.fa.finish();
                Toast.makeText(PreApprove.this, "Setup Completed Successfully!", Toast.LENGTH_LONG).show();
                //PreApprove.this.finish();
                Payment.pay(PreApprove.this);
            }// GOOD APPROVED
            if (url.contains("www.yahoo.com")) {
                PreApprove.this.finish();
            }// BAD CANCELLED

            view.loadUrl(url);

            return true;

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // NEVER GETS CALLED WHEN PAYPAL RELOADS :-(
        }
    });
    wv.loadUrl("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-preapproval&preapprovalkey="
            + preappovalkey);

}

Upvotes: 4

Views: 4093

Answers (2)

Wei WANG
Wei WANG

Reputation: 1778

Did you try WebViewClient.shouldInterceptRequest(WebView view, String url)?

And you may also want to take a look into the paypal web page to see what happens after clicking the button. e.g. Is it an AJAX request?

----EDIT----

Please also note that WebViewClient.shouldOverrideUrlLoading() is not always called for each request. As far as I know, it won't at least for the following cases:

  1. Intrapage navigation. For example, two URLs with only different URL fragments.
  2. POST requests.
  3. Requests from an iframe.

Also it's documented in Android doc: It's called ...when a new url is about to be loaded in the current WebView.

Regarding onPageStarted/Finished(), it will only be called when the main html/document is reloaded. i.e. it will not get called in case of AJAX requests.

Upvotes: 1

Daan Oerlemans
Daan Oerlemans

Reputation: 245

You can use this in the same Client as shouldOverrideUrlLoading:

Link

@Override
public void onPageFinished(WebView view, String url) {
}

If you want to detect when the page finished loading.

Or you can use:

Link

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}

To detect when a page starts loading.

Upvotes: 1

Related Questions