Android WebView navigating event?

2019-06-21 04:19发布

问题:

I'm looking for the event of a Android WebView that is equivalent to the "Navigating" event of a WPF WebBrowser, or the "ShouldStartLoad" of a IOS UiWebView.

I found "shouldOverrideUrlLoading", but I don't know how to use it because the constructor needs an URL...

I would like to intercept the Url before navigating, and cancel the navigation for some URLs...

I'm using Xamarin in Visual Studio.

WPF equivalent :

webBrowser.Navigating += WebBrowserNavigating;

private void WebBrowserNavigating(object sender, NavigatingEventArgs e)
    {
        if (//condition)
        e.Cancel = true;
        //Do something else
    }

回答1:

Yes, you should use the shouldOverrideUrlLoading method. This method is called automatically, so you don't have to worry about the parameters. In order to use this method, you have to create first a WebViewClient like this:

private class HelloWebViewClient extends WebViewClient {

     @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
         ...
      }
}

And then, you set it as your WebViewClient:

webview.setWebViewClient(new HelloWebViewClient());

When the shouldOverrideUrlLoading method is called you'll receive by parameter the WebView and the URL that is going to be loaded.