Solve the problem where webview calls goBack () and returns the previous page to automatically refresh the flash white. webviewgoback

Source: Internet
Author: User

Solve the problem where webview calls goBack () and returns the previous page to automatically refresh the flash white. webviewgoback

The problem is that the onKeyDown () method is rewritten to make it goBack (), but the problem is that each time you enter webview and return the original page again, there will be a flash in the middle.

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

Later, I realized that there is a cache mode in webView. How can I use different cache modes?

// Set the cache Mode
WebView. getSettings (). setCacheMode (WebSettings. LOAD_DEFAULT );
You need to change the mode to LOAD_NO_CACHE !! However, every time you have to re-request a very expensive resource, you can put it in onKeyDown () to re-write a LOAD_NO_CACHE, and only call the no-Cache mode when returning
In this way, when the previous page is returned, the Code becomes transient:
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);}/* rewrite the logic returned by the physical key */@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK & webview. canGoBack () {// returns the webview on the previous page. getSettings (). setCacheMode (WebSettings. LOAD_NO_CACHE); webview. goBack (); return true;} return super. onKeyDown (keyCode, event );}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.