From 16a4e91aac2cfcd0a8b3954fbf92789255e944f1 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Mon, 24 Sep 2018 05:46:32 -0600 Subject: [PATCH] =?UTF-8?q?Ensure=20that=20the=20ListViewAdapter=20is=20no?= =?UTF-8?q?=20longer=20being=20used=20by=20the=20ListVi=E2=80=A6=20(#3848)?= =?UTF-8?q?=20fixes=20#3603?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Ensure that the ListViewAdapter is no longer being used by the ListView before disposing it * Remove `RemoveAllViews` from `Dispose` --- .../Renderers/ListViewAdapter.cs | 8 ++++++++ .../Renderers/ListViewRenderer.cs | 12 ++++++++++++ .../VisualElementRenderer.cs | 2 -- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs b/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs index 50793d373..4c922489a 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs @@ -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(); diff --git a/Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs index a39147db9..241084ab2 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs @@ -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; } diff --git a/Xamarin.Forms.Platform.Android/VisualElementRenderer.cs b/Xamarin.Forms.Platform.Android/VisualElementRenderer.cs index c5f7b5125..8b8eede47 100644 --- a/Xamarin.Forms.Platform.Android/VisualElementRenderer.cs +++ b/Xamarin.Forms.Platform.Android/VisualElementRenderer.cs @@ -238,8 +238,6 @@ namespace Xamarin.Forms.Platform.Android } } - RemoveAllViews(); - if (Element != null) { Element.PropertyChanged -= _propertyChangeHandler;