androidwebviewwebviewclient

Android WebViewClient callbacks called too often


When I call WebView#loadUrl I expect that I should only get a single WebViewClient#onPageFinished call and no WebViewClient#shouldOverrideUrlLoading call. However, I get a WebViewClient#shouldOverrideUrlLoading (which I implement by always calling WebView#loadUrl and returning true) and then two WebViewClient#onPageFinished calls with the same URL.

The page I'm loading uses a lot of ajax requests. Do ajax requests invoke the WebViewClient? My page does not have any meta-refreshes in it.

This is quite frustrating. Am I doing something wrong?


Solution

  • This is a cross-post from a google groups discussion I started on the topic.

    I've done more research, and I think I understand everything that's going on.

    Here is my WebViewClient:

    public class MyWebViewClient extends WebViewClient {
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
        System.out.println("onPageStarted: " + url);
      }
    
      @Override
      public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        System.out.println("shouldOverrideUrlLoading: " + url);
        webView.loadUrl(url);
        return true;
      }
    
      @Override
      public void onPageFinished(WebView webView, String url) {
        System.out.println("onPageFinished: " + url);
      }
    }
    

    I have the following URL redirects from my server:

    http://example.com/resource -> http://example.com/user-name/resource
    http://example.com/logout.jsp -> http://example.com/login/logout
    http://example.com/login/logout -> http://example.com/login
    

    These URLs are part of a server that I don't control, so I just have to deal with the behavior.

    Here is the output from loading http://example.com/resource

    onPageStarted: http://example.com/resource
    onPageStarted: http://example.com/user-name/resource
    shouldOverrideUrlLoading: http://example.com/user-name/resource
    onPageFinished: hhttp://example.com/user-name/resource
    

    at this point, my WebView has blank content. I must wait until after the second onPageFinished to grab my content.

    onPageStarted: http://example.coms/user-name/resource
    onPageFinished: http://example.com/user-name/resource
    

    Here is the output from loading http://example.com/logout.jsp

    onPageStarted: http://example.com/logout.jsp
    onPageStarted: http://example.com/login/logout
    shouldOverrideUrlLoading: http://example.com/login/logout
    onPageFinished: http://example.com/login/logout
    onPageStarted: http://example.com/login/logout
    onPageStarted: http://example.com/login
    shouldOverrideUrlLoading: http://example.com/login
    onPageFinished: http://example.com/login
    

    again, at this point, my WebView has a blank page. I have to wait until the 3rd onPageFinished to get my content from the WebView.

    onPageStarted: http://example.com/login
    onPageFinished: http://example.com/login
    

    Based on the documentation, I don't expect this behavior. Notice that there is an unbalanced number of onPageStarteds and onPageFinisheds. I especially dislike how onPageFinished gets called with my redirected URL, yet the WebView doesn't contain my content. I understand that this behavior is probably unchangeable, but this unexpected behavior should at least be documented.