问题描述
我使用的WebViewClient.shouldOverrideUrlLoading捕捉任何链接的点击次数在web视图。这工作约95%的时间,但有时它只是不叫。
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:
- 当链接指向的网页,他们已经在。这个人是不是一个真正的大问题,而且有报道为它错误。
- 当页面仍在加载,如果用户点击一个链接,它很少捕获它。它只是将打开该链接而没有调用
shouldOverrideUrlLoading
。 - 随机。虽然这个实验在过去的几个月我已经注意到,有时它只是没有抓住它。这是罕见的,但它确实发生了。
- 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.
- 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
. - 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.
现在我的问题主要是如何解决问题2,既然别人都少了一个大问题。但是,如果有人已经解决了1或3我很想听到它。
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.
另外,我要指出,我在 shouldOverrideUrlLoading
尝试不同的返回值和我注意到,总是返回true,似乎有最好的结果,但上面仍是问题发生了。
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.
推荐答案
如果HTML是你自己的,你可以替换调用自己的Java对象,注入的WebView 所有的传统联系code>通过
addJavascriptInterface()
。然后,你将不再依赖于 shouldOverrideUrlLoading()
。
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()
.
否则,也许尝试prevent用户从与页面交互,直到 onPageFinished()
。
Otherwise, perhaps try to prevent the user from interacting with the page until onPageFinished()
.
我还没有遇到这个问题,但我还没有广泛使用的WebView
与任意内容,无论是。
I have not run into this problem, but I have not made extensive use of WebView
with arbitrary content, either.
这篇关于shouldOverrideUrlLoading不工作/捕获链接点击,而页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!