-1

I'm using a standard WebView implementation, and overriding the shouldOverrideUrlLoading method to catch request to external domains. The call is being captured on all of my tested versions (15-22); however,on 15-18, the WebView navigates to the requested URL before shouldOverrideUrlLoading is called to execute the External Browser request.

Example:

SDK >= 19

WebView -> Load URL -> shouldOverrideUrlLoading(TRUE) -> URL loaded in External Browser and WebView's state is retained.

SDK <= 18

WebView -> Load URL -> URL loaded in WebView -> shouldOverrideUrlLoading(TRUE) -> URL loaded in External Browser and WebView's state is lost.

WebView Override Code:

private void webViewClient() {
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith(BASE_URL)) {
                return false;
            } else {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(i);
                return true;
            }
        }
    });
}
Evan Bashir
  • 5,501
  • 2
  • 25
  • 29

1 Answers1

1

One possible cause:
This behavior makes sense if the URL being loaded is "invalid" (like something other than "http://whatever.com/"), and is somehow also a "redirect".

If the invalid url being loaded is a "redirect"... >=19 will not call the shouldOverrideUrlLoading at all.

If the FINAL URL is valid, however, and doesn't start with BASE_URL, it would call shouldOverrideUrlLoading, then launch the new window, as your code says.

That said, I have no idea how you would get an invalid URL to be a redirect -- so without more information about the URLs (BASE_URL and the URL being requested), it's impossible to say.

Read more about the differences between the WebView in 19+... big changes were made at that time:

https://developer.android.com/guide/webapps/migrating.html

RAndy-roid
  • 11
  • 1