Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shouldOverrideUrlLoading does not work/catch link clicks while page is loading

I am using WebViewClient.shouldOverrideUrlLoading to catch any link clicks in the WebView. This works about 95% of the time, but sometimes it is simply not called.

I have noticed it in these three circumstances:

  1. When the link points to the page they are already on. This one isn't a really big deal, and there is a bug reported for it.
  2. While the page is still loading, if the user clicks a link, it rarely catches it. It will just open the link without ever calling shouldOverrideUrlLoading.
  3. Randomly. While experimenting with this over the last couple months I have noticed that sometimes it just doesn't catch it. It is rare but it does happen.

Now my question is mostly how to fix problem 2, since the others are less of a big deal. However, if someone has solved either 1 or 3 I would love to hear about it.

Also, I should mention that I have tried different return values in shouldOverrideUrlLoading and I have noticed that always returning true seems to have the best result, but the issues above still happen.

like image 264
cottonBallPaws Avatar asked Feb 26 '11 19:02

cottonBallPaws


2 Answers

If the HTML is your own, you can replace all traditional links with calls to your own Java object, injected into the WebView via addJavascriptInterface(). Then you will no longer be reliant upon shouldOverrideUrlLoading().

Otherwise, perhaps try to prevent the user from interacting with the page until onPageFinished().

I have not run into this problem, but I have not made extensive use of WebView with arbitrary content, either.

like image 165
CommonsWare Avatar answered Oct 02 '22 13:10

CommonsWare


I've had a similar issue when trying to change the webpage with js on the page that modifies window.location. Turns out that modifying the url scheme/protocol makes the webview catch on shouldOverrideUrlLoading. I know this doesn't help you since you don't own the HTML, but I thought it would help anyone else who stumbles upon this problem.

like image 21
Grantland Chew Avatar answered Oct 02 '22 15:10

Grantland Chew