0

I am currently making a xamarin application that contains a listview which when clicked opens a generated pdf using google.docs in a webview (Not the cleanest way but works for what I need). The PDF is opened using rg.plugin popup window that contains the webview. When the user is done viewing the pdf they can exit and return to the listview page.

Everything works correctly but once I have opened/closed 4/5 pdfs the webview starts to show just a blank screen. I believe this must be an error in the webview/application holding onto those pdfs rather than releasing that memory.

What I'm asking really is, what can be done to mitigate this issue be it cleaning up after the popup window is closed or something else?

Many thanks

JakeDunbar
  • 31
  • 4

2 Answers2

1

This is a similar problem occured to me. This is an issue with Android Webview not with Xamarin. You can see my question Android Webview cannot render the pdf sometimes and shows blank/white page instead

You can solved the Problem in the two ways. 1. One is to use the Js.Pdf Plugin on the server end. 2. Second option is not the recommended one. You can recursively called the function to load webview. Below is the code:

private void showPdf(final String imageString) {
pdfView.invalidate();
pdfView.getSettings().setJavaScriptEnabled(true);
pdfView.getSettings().setSupportZoom(true);
pdfView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + imageString);
pdfView.setWebViewClient(new WebViewClient() {
    boolean checkhasOnPageStarted = false;

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {

    }

    @Override
    public void onPageFinished(WebView view, String url) {
       if(view.Title == ""){
           view.Reload();
    }
});}
Gopal Awasthi
  • 423
  • 1
  • 6
  • 15
1

I managed to solve my issue building from what you said before just thought I'd post my solution incase it's useful to anyone else looking for this.

In my custom webview renderer android I created a custom webviewclient that used onPageCommitVisible to check if the page had loaded correctly. If the load wasn't successful the page is loaded again solving the issue.

class CustomWebViewClient : WebViewClient
{
        bool pageCommitted = false;

        public override void OnPageFinished(Android.Webkit.WebView view, string url)
        {

            if (!pageCommitted)
            {
                view.LoadUrl(url);
            }

            base.OnPageFinished(view, url);
        }

        public override void OnPageCommitVisible(Android.Webkit.WebView view, string url)
        {
            pageCommitted = true;
        }
}

Attached that to my OnElementChanged event

protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
    {
        base.OnElementChanged(e);

        //Control.LoadUrl("about:blank");

        if (Control == null)
        {
            var webView = new Android.Webkit.WebView(_context);
            webView.Settings.SetSupportZoom(true);
            webView.Settings.BuiltInZoomControls = true;
            webView.Settings.DisplayZoomControls = false;
            webView.Settings.JavaScriptEnabled = true;
            webView.Settings.SetEnableSmoothTransition(true);
            webView.SetWebViewClient(new CustomWebViewClient());
            SetNativeControl(webView);
            Control.LoadUrl(Element.Uri);
        }
    }
JakeDunbar
  • 31
  • 4
  • This Solution works for you. That is great i have update the latest solution that worked 100%. if you want you can try that also! – Gopal Awasthi May 06 '20 at 05:26