shouldOverrideUrlLoading返回值

前言

这周在做一个需求时,需要用到WebView,但是在做的过程中,却遇到了一些问题。WebView我其实接触并不多,这次的需求里还涉及了一些JS交互之类的,所以我是边学边做,但是网上的示例和教程,往往藏着一些坑,这一次我就发现关于shouldOverrideUrlLoading这个方法,网上的说法真的是五花八门,这篇文章,我们来澄清一下shouldOverrideUrlLoading的真正用法。


官方文档:

 /**
     * Give the host application a chance to take over the control when a new
     * url is about to be loaded in the current WebView. If WebViewClient is not
     * provided, by default WebView will ask Activity Manager to choose the
     * proper handler for the url. If WebViewClient is provided, return true
     * means the host application handles the url, while return false means the
     * current WebView handles the url.
     * This method is not called for requests using the POST "method".
     *
     * @param view The WebView that is initiating the callback.
     * @param url The url to be loaded.
     * @return True if the host application wants to leave the current WebView
     *         and handle the url itself, otherwise return false.
     * @deprecated Use {@link #shouldOverrideUrlLoading(WebView, WebResourceRequest)
     *             shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead.
     */

大致翻译一下:

  • 若没有设置 WebViewClient 则由系统(Activity Manager)处理该 url,通常是使用浏览器打开或弹出浏览器选择对话框。
  • 若设置 WebViewClient 且该方法返回 true ,则说明由应用的代码处理该 url,WebView 不处理,也就是程序员自己做处理。
  • 若设置 WebViewClient 且该方法返回 false,则说明由 WebView 处理该 url,即用 WebView 加载该 url。

     WebView的前进、后退、刷新、以及post请求都不会调用shouldOverrideUrlLoading方法,除去以上行为,还得满足( ! isLoadUrl || isRedirect) 即 (不是通过webView.loadUrl来加载的 或者 是重定向) 这个条件,才会调用shouldOverrideUrlLoading方法。




参考链接:https://www.jianshu.com/p/3474cb8096da 谢谢作者

发布了29 篇原创文章 · 获赞 49 · 访问量 6606

猜你喜欢

转载自blog.csdn.net/wangsen927/article/details/97812742