Xamarin.Forms
                                
                                 Xamarin.Forms copied to clipboard
                                
                                    Xamarin.Forms copied to clipboard
                            
                            
                            
                        [Bug] Cannot access a disposed object. Object name: 'Android.Webkit.WebView'.'
Description
System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'Android.Webkit.WebView'.'
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.ObjectDisposedException: Cannot access a disposed object.
[mono-rt] Object name: 'Android.Webkit.WebView'.
[mono-rt]   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.69(intptr,intptr,intptr,intptr,intptr)
[mono-rt]   at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.69(intptr,intptr,intptr,intptr,intptr)
[BpBinder] onLastStrongRef automatically unlinking death recipients: 
Steps to Reproduce
Open a web page Execute an async javascript functions Click on button back navigation Repeat these steps quickly 3 or 4 times or until crash happen
Xamarin.Forms 5.0.0.2012
For navigation use Shell or NavigationPage?, could you attach a small sample where reproduce the issue?. Thanks!
I also have this problem, how to solve it
Sorry for the late reply @jsuarezruiz
it is an NavigationPage let me picture some scenario , suppose I have two different pages
- MainPage
- WebViewPage
to catch this issue you need to speed up the process of clicking , for example open the WebViewPage and then click on back directly while the page in construction process , that is it
Hello @SolarisYan how did u face this issue ! is there an specific behavior !!
Hi, I have the same scenario.
I have a very simple WebViewPage, like below:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.Views.ContactUs"> <ContentPage.Content> <StackLayout> <WebView x:Name="webView" Source="https://www.microsoft.com/" VerticalOptions="FillAndExpand" Navigated="webviewNavigated" Navigating="webviewNavigating"/> </StackLayout> </ContentPage.Content> </ContentPage>
From my MainPage (or any other page) I click a button and call this page like below
await Shell.Current.GoToAsync("ContactUs");
If I wait to the entire page to be loaded, everything works great, but if I click back button without wait, after 3 or 4 times, the app crash with the message:
Cannot access a disposed object. Object name: 'Android.Webkit.WebView'.
I hope this help to solve this issue. Any workaround will be very appreciated.
For navigation use Shell or NavigationPage?, could you attach a small sample where reproduce the issue?. Thanks!
@jsuarezruiz i hope my post could help
Same problem...
Below is the stack trace I'm seeing reported.
@jsuarezruiz Could we maybe add some defensive code around this line to make it more stable? It looks like it's crashing in the accessor for Control.Url? I wonder if it's as simple as checking whether Control has been disposed before we try to access Url?
https://github.com/xamarin/Xamarin.Forms/blob/94acebbb4bee16bf71338ef6906b308ca08524c2/Xamarin.Forms.Platform.Android/Renderers/FormsWebViewClient.cs#L101
JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self)
JniPeerMembers+JniInstanceMethods.InvokeVirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters)
WebView.get_Url ()
FormsWebViewClient.OnReceivedError (Android.Webkit.WebView view, Android.Webkit.IWebResourceRequest request, Android.Webkit.WebResourceError error)
WebViewClient.n_OnReceivedError_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_Landroid_webkit_WebResourceError_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_view, System.IntPtr native_request, System.IntPtr native_error)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.16(intptr,intptr,intptr,intptr,intptr)
Did anyone manage to find fix?
Having same issue as well.
@kubalobacz @dzebas @jsuarezruiz i Have found a workaround
[assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewCustomRenderer))]
namespace <yournamespace>.Droid
{
    public class CustomWebViewCustomRenderer : WebViewRenderer
    {
        public CustomWebViewCustomRenderer(Context context) : base(context)
        {
        }
       protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                View = e.NewElement as CustomWebView;
                var webView = Control;
                  webView.SetWebViewClient(new MyWebViewClient(this));
               }
            }
        }
public class MyWebViewClient : FormsWebViewClient
        {
            public MyWebViewClient(WebViewRenderer renderer) : base(renderer)
            {
            }
            public override void OnReceivedError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceError error)
            {
                try
                {
                    base.OnReceivedError(view, request, error);
                }
                catch (System.Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
                
            }
        }