Article From:https://www.cnblogs.com/ganchuanpu/p/6545838.html

The problem is: Rewriting the onKeyDown () method to make it goBack (), but the problem is that every time you go back to the WebView and return to the original page, there will be a flash in the middle.

I tried a lot of online methods and found that they were all nonsense.Injustice

Later, I realized that webView has caching mode in cache. How can we use different caching modes?

//Setting the cache mode
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
You need to change the pattern to LOAD_NO_CACHE!
But each time you have to re request, it costs a lot of resources, so you can rewrite a LOAD_NO_CACHE in onKeyDown (), and invoke the caching mode only when you return.
This way, you can refresh your whiteness without returning to the previous page.
Code:

private void initView() {  
    webview = (WebView) findViewById(R.id.faq_wv);  
    webview.loadUrl(url);  
    webview.setWebViewClient(new WebViewClient() {  
        @Override  
        public boolean shouldOverrideUrlLoading(WebView view, String url) {  
            view.loadUrl(url);  
            return true;  
        }  
    });  
    WebSettings settings = webview.getSettings();  
    settings.setJavaScriptEnabled(true);  
    settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  
}  
  
/* Rewriting the logic of the physical buttonsSuch@OverridePublic Boolean Boolean onKeyDown (int keyCode, KeyEvent KeyEvent event)If (KE)YCode = = = KeyEvent.KEYCODE_BACK; & & webview.canGoBack ()) {Return to the previous pageWebvIew.getSettings ().SetCacheMode (WebSettings.LOAD_NO_CACHE);Webview.goBack ();RetUrn true;    }  Return super.onKeyDown (keyCode, event)}

  

Leave a Reply

Your email address will not be published. Required fields are marked *