Android Cached WebView

CodeSenseiCodeSensei USMember ✭✭
edited May 17 in Xamarin.Forms

When I try to load cached webview on Droid, the webview partially loads some parts of the cached content, instead of showing the whole cached webview content.

But strangely, I can fully load the cached webview every other time I load the webview.

Anyone experience this?

protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
{
    var webView = new Android.Webkit.WebView(_context);
    webView.Settings.JavaScriptEnabled = true;
    webView.SetWebViewClient(new MyWebViewClient());
    webView.SetWebChromeClient(new WebChromeClient());

    if (CrossConnectivity.Current.IsConnected)
    {
        // Get from new web request
        webView.Settings.CacheMode = CacheModes.NoCache;
    }
    else
    {
        // Read from cache
        webView.Settings.CacheMode = CacheModes.CacheOnly;
    }
}


public class MyWebViewClient : WebViewClient
{
    public override void OnPageStarted(Android.Webkit.WebView view, string url, Android.Graphics.Bitmap favicon)    
    {
        base.OnPageStarted(view, url, favicon);
    }

    public override void OnPageFinished(Android.Webkit.WebView view, string url)
    {
        shareView = view;
        webViewWidth = view.Width;
        webViewHeight = view.Height;
    }
}

Answers

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    What is the mean of every other time?

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    You can refer to the following code .It works fine on my side

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
    {
      base.OnElementChanged(e);
    
      if(Control!=null)
       {
          var webView = Control as Android.Webkit.WebView;
    
          webView.Settings.JavaScriptEnabled = true;
          webView.Settings.SetAppCacheEnabled(true);
    
          webView.SetWebViewClient(new MyWebViewClient());
          webView.SetWebChromeClient(new WebChromeClient());
    
          if (CrossConnectivity.Current.IsConnected)
          {
             // Get from new web request
            webView.Settings.CacheMode = CacheModes.Default;
           }
    
           else
           {
                // Read from cache
                webView.Settings.CacheMode = CacheModes.CacheElseNetwork;
            }
         }
    }
    
  • CodeSenseiCodeSensei USMember ✭✭

    Example of "every other time" is when the webview is loaded, the view works. If you try to to load the webview again, the view does not load. If you try to load the webview again, the view works.

    Think your example code should be :

    if(Control ==null)
    {
    // Then instantiate the webview
    ............

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    Did you try my code?

  • CodeSenseiCodeSensei USMember ✭✭

    Lucas,

    Your code will never be run because the Control is always null.

    Ive came to the conclusion that the javascript(very heavy js page) that gets loaded is interfering with the webview. Ive tried loading a simple website like google.com, and cache works fine.

Sign In or Register to comment.