Ensure that the ListViewAdapter is no longer being used by the ListViā€¦ (#3848) fixes...
authorE.Z. Hart <hartez@users.noreply.github.com>
Mon, 24 Sep 2018 11:46:32 +0000 (05:46 -0600)
committerRui Marinho <me@ruimarinho.net>
Mon, 24 Sep 2018 11:46:32 +0000 (12:46 +0100)
* Ensure that the ListViewAdapter is no longer being used by the ListView before disposing it

* Remove `RemoveAllViews` from `Dispose`

Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs
Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs
Xamarin.Forms.Platform.Android/VisualElementRenderer.cs

index 50793d3..4c92248 100644 (file)
@@ -15,6 +15,7 @@ namespace Xamarin.Forms.Platform.Android
 {
        internal class ListViewAdapter : CellAdapter
        {
+               bool _disposed;
                static readonly object DefaultItemTypeOrDataTemplate = new object();
                const int DefaultGroupHeaderTemplateId = 0;
                const int DefaultItemTemplateId = 1;
@@ -417,6 +418,13 @@ namespace Xamarin.Forms.Platform.Android
 
                protected override void Dispose(bool disposing)
                {
+                       if (_disposed)
+                       {
+                               return;
+                       }
+
+                       _disposed = true;
+
                        if (disposing)
                        {
                                CloseContextActions();
index a39147d..241084a 100644 (file)
@@ -75,6 +75,12 @@ namespace Xamarin.Forms.Platform.Android
                                _footerView?.Dispose();
                                _footerView = null;
 
+                               // Unhook the adapter from the ListView before disposing of it
+                               if (Control != null)
+                               {
+                                       Control.Adapter = null;
+                               }
+
                                if (_adapter != null)
                                {
                                        _adapter.Dispose();
@@ -124,6 +130,12 @@ namespace Xamarin.Forms.Platform.Android
 
                                if (_adapter != null)
                                {
+                                       // Unhook the adapter from the ListView before disposing of it
+                                       if (Control != null)
+                                       {
+                                               Control.Adapter = null;
+                                       }
+
                                        _adapter.Dispose();
                                        _adapter = null;
                                }
index c5f7b51..8b8eede 100644 (file)
@@ -238,8 +238,6 @@ namespace Xamarin.Forms.Platform.Android
                                        }
                                }
 
-                               RemoveAllViews();
-
                                if (Element != null)
                                {
                                        Element.PropertyChanged -= _propertyChangeHandler;