From 88bdba49b8f644a8db8e62663fa3e787caefc4eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Mon, 23 May 2022 12:13:17 +0200 Subject: [PATCH] Fix crash removing items in ListView --- src/Core/src/Handlers/Layout/LayoutHandler.Android.cs | 2 +- src/Core/src/Platform/Android/ViewGroupExtensions.cs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Core/src/Handlers/Layout/LayoutHandler.Android.cs b/src/Core/src/Handlers/Layout/LayoutHandler.Android.cs index 8c23875d2d62..84f59759f432 100644 --- a/src/Core/src/Handlers/Layout/LayoutHandler.Android.cs +++ b/src/Core/src/Handlers/Layout/LayoutHandler.Android.cs @@ -40,7 +40,7 @@ public override void SetVirtualView(IView view) foreach (var child in VirtualView.OrderByZIndex()) { - PlatformView.AddView(child.ToPlatform(MauiContext)); + PlatformView.SafelyAddView(child.ToPlatform(MauiContext)); } } diff --git a/src/Core/src/Platform/Android/ViewGroupExtensions.cs b/src/Core/src/Platform/Android/ViewGroupExtensions.cs index 8940e5585173..b8bc13aef2df 100644 --- a/src/Core/src/Platform/Android/ViewGroupExtensions.cs +++ b/src/Core/src/Platform/Android/ViewGroupExtensions.cs @@ -6,6 +6,14 @@ namespace Microsoft.Maui.Platform { public static class ViewGroupExtensions { + public static void SafelyAddView(this AViewGroup viewGroup, AView child) + { + if (child.Parent != null) + child.RemoveFromParent(); + + viewGroup.AddView(child); + } + public static IEnumerable GetChildrenOfType(this AViewGroup viewGroup) where T : AView { for (var i = 0; i < viewGroup.ChildCount; i++)