Solve the problem that the page does not refresh when webview calls loadUrl for the second time

  • 2021-11-29 08:28:39
  • OfStack

1 demand, and when you click the Button button, you want to load another Url.

The following methods can succeed!


@Override
public void onClick(View view) {  
  webview.loadUrl(url);
  webview.loadUrl( "javascript:window.location.reload( true )" ) ; 
}

Pro-test works!

Supplementary knowledge: misunderstanding of WebView. loadUrl

When using loadUrl to load web pages, sometimes the phenomenon of calling the system browser to load web pages will appear. Most of the online solutions are:


webView.setWebViewClient(new WebViewClient() {
  public boolean shouldOverrideUrlLoading(WebView view, String url)
  { 
    view.loadUrl(url);
    return true;
  }
}

This does do the same thing as loading a Web page in the current webview, but it does extra work and returns unreasonable values.

In fact, if you just need to avoid starting the system browser to load the page, just write it like this

webView.setWebViewClient(new WebViewClient());

There is no need to write another class to inherit WebViewClient and then override its methods.

When the application scenario is more complex, you can find the disadvantages of the methods provided online:

url has redirection and cannot be rolled back

shouldOverrideUrlLoading(WebView view, String url)

Determines whether the url is automatically processed by the webview, that is, whether it is loaded. When true is returned, it is handled by the program, and when false is returned, webview will handle it itself, which is equivalent to automatically executing loadUrl method.


Related articles: