Xamarin WebView on Android showing multiple instances

1.2k Views Asked by At

I have created a Hybrid App using Xamarin.Forms. It uses multiple WebViews (XLabs HybridWebViews). I use the same app on iOS and it works fine, while on Android it gradually slows down. When I do a chrome://inspect on the device it shows multiple WebViews for the same page, seems more like the WebViews which were hidden are still in the memory and are slowing down the application.

How can I destroy the hidden WebView instances on Android?

Following is a screenshot of the chrome://inspect on the App:

enter image description here

1

There are 1 best solutions below

1
On

Generally you should:

  • set your WebViews instances to null.
  • (optionaly) force garbage collection with GC.Collect();

If it's not enough make a custom renderer of your WebView and add this:

protected override void Dispose(bool disposing)
{
    if (disposing && Element != null)
    {
        if (Control != null)
        {
                Control.StopLoading();
                Control.ClearHistory();
                Control.ClearCache(false);
                Control.LoadUrl("about:blank");
                Control.FreeMemory();
                Control.PauseTimers();
        }
    }

    base.Dispose(disposing);
}

It should solve your issues.