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?

Answers

  • 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();
                _webView.SetWebViewClient(custWebViewClient);
                var webChromeClient = new WebChromeClient();
                _webView.SetWebChromeClient(webChromeClient);
                //var hardwareAccelerated = _webView.IsHardwareAccelerated;
                _webView.Settings.JavaScriptEnabled = true;
                _webView.Settings.CacheMode = CacheModes.CacheElseNetwork;
                _webView.Settings.SetPluginState(WebSettings.PluginState.On);
                _webView.Settings.SetAppCacheEnabled(true);
                _webView.Settings.SetAppCacheMaxSize(2048000);
                _webView.Settings.AllowFileAccess = true;
                _webView.Settings.JavaScriptCanOpenWindowsAutomatically = true;
                _webView.Settings.SetRenderPriority(WebSettings.RenderPriority.High);
                _webView.Settings.SetLayoutAlgorithm(WebSettings.LayoutAlgorithm.SingleColumn);
                _webView.Settings.SetSupportMultipleWindows(true);
                _webView.Settings.DomStorageEnabled = true;
                _webView.Settings.DefaultTextEncodingName = "utf-8";
    
                _webView.SetLayerType(LayerType.Hardware, new Paint());
                _webView.LoadUrl(_url);
            }
    
            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

    Thanks

  • AceLvAceLv USMember

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

  • dpreznikdpreznik Member ✭✭

    Can WebView be stopped for UWP?

  • Stef788Stef788 Member

    Yes, into a ContentPage this works :

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