15

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.

erdemlal
  • 491
  • 5
  • 21
cottonBallPaws
  • 21,220
  • 37
  • 123
  • 171
  • 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`. – Grantland Chew Jul 07 '11 at 21:23
  • You say, in a comment on the accepted answer, that the cause of the problem was in your logic. You never mention what the real problem was. I have this problem, and from googling it seems i'm not the only one. Would you care to spare a moment and tell us about how you solved it? – mickey Feb 02 '12 at 18:59
  • @mickey, thanks for pointing this out. Sorry about that! I wrote a response below. (In the accepted answers comments) – cottonBallPaws Feb 03 '12 at 06:25
  • Np! Too bad though that it seems as if my problem isn't the same as yours... :( – mickey Feb 05 '12 at 09:37
  • @mickey, what is happening for you? Pretty much as described in this original question or different? – cottonBallPaws Feb 06 '12 at 05:51
  • A bit different. But what you tried to do might have turned out to be a solution to my problem. If you have a spare moment, you're more than welcome to read my question, it remains unanswered at http://stackoverflow.com/questions/9147875/webview-dont-display-javascript-windows-open and at least leave a comforting comment ;) – mickey Feb 06 '12 at 06:17

2 Answers2

7

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thanks for the suggestions. Unfortunately the HTML in this case is not my own and I can't block the user because some pages are "loading" for a while after they are quite usable to the user. So that would be a pretty frustrating UI experience. It is pretty much 100% reproducible for me. Load a url into the WebView and while it is loading, press a link, and it just skips over the override call. – cottonBallPaws Mar 03 '11 at 04:49
  • This ended up being a flaw in some logic I had that I didn't catch. I'll award the answer to you because you had some good suggestions anyhow. Thanks. – cottonBallPaws Mar 07 '11 at 23:31
  • 1
    @littleFluffyKitty what are the flaws in the logic you're talking about? You should write them up. – ThomasW Sep 14 '11 at 01:28
  • 2
    @ThomasW It was a long time ago but I'm pretty sure I found a place where I was returning false. I believe you must absolutely always return true in shouldOVerrideUrlLoading() to avoid this problem. In that method, if you find you don't need to override the load, just call webview.loadUrl(url) instead of returning false. – cottonBallPaws Feb 03 '12 at 06:25
5

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.

Grantland Chew
  • 2,620
  • 26
  • 26