Forum Xamarin.Android

How to stop a Webview

Hi, I'm using a web view to play videos, but when I navigate away from the page the audio won't stop. I have came across a few pages here, and many on StackOverflow but they weren't much help.

People at SO suggests that I should be overriding the OnPause method from my mainactivity, Tried that and it doesn't get called. So I managed to catch when the user navigates away from the page. From then I'm calling a method to clear MyWebViews.source to null, calling GC. But the Audio won't stop.

Can anyone suggest how I can destroy the Webview? Or explain how the Android lifecycle works in accordance to Webviews?


  • AceLvAceLv USMember

    hi, the following solution is for Android:
    in your activity:
    Window.SetFlags(WindowManagerFlags.HardwareAccelerated, WindowManagerFlags.HardwareAccelerated);
    and your webview:
    if (_webView == null)
    _webView = new WebView(Activity) {Background = new ColorDrawable(Color.Transparent)};

                var custWebViewClient = new WebViewClient();
                var webChromeClient = new WebChromeClient();
                //var hardwareAccelerated = _webView.IsHardwareAccelerated;
                _webView.Settings.JavaScriptEnabled = true;
                _webView.Settings.CacheMode = CacheModes.CacheElseNetwork;
                _webView.Settings.AllowFileAccess = true;
                _webView.Settings.JavaScriptCanOpenWindowsAutomatically = true;
                _webView.Settings.DomStorageEnabled = true;
                _webView.Settings.DefaultTextEncodingName = "utf-8";
                _webView.SetLayerType(LayerType.Hardware, new Paint());
            return _webView;

    and, don't test on simulators but on real devices. GOOD LUCK!

  • OzanKeskin.7430OzanKeskin.7430 GBMember ✭✭

    Hi @AceLv
    Thank you for the reply, I was on holiday so I just saw this.

    web view doesn't have an extension of Settings on mine. Are you using some package for that? Also the same with WebViewClient


  • AceLvAceLv USMember

    Xamarin Android(Native in PCL)
    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v6.0\Mono.Android.dll
    using Android.Webkit;

  • dpreznikdpreznik Member ✭✭

    Can WebView be stopped for UWP?

  • Stef788Stef788 Member

    Yes, into a ContentPage this works :

        protected override bool OnBackButtonPressed()
                this.Player.Source = new UrlWebViewSource() { Url = "about:blank" };
            catch (Exception)
            return base.OnBackButtonPressed();
Sign In or Register to comment.