diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issue19786.xaml b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue19786.xaml
new file mode 100644
index 000000000000..195ed2f36e8d
--- /dev/null
+++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue19786.xaml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issue19786.xaml.cs b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue19786.xaml.cs
new file mode 100644
index 000000000000..a6afed72ebc3
--- /dev/null
+++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue19786.xaml.cs
@@ -0,0 +1,61 @@
+using System.Collections.ObjectModel;
+using Microsoft.Maui.Controls;
+using Microsoft.Maui.Controls.Xaml;
+
+namespace Maui.Controls.Sample.Issues
+{
+ [XamlCompilation(XamlCompilationOptions.Compile)]
+ [Issue(IssueTracker.Github, 19786, "[Android] Crash removing item from CarouselView", PlatformAffected.All)]
+ public partial class Issue19786 : ContentPage
+ {
+ public Command AddItemCommand { get; set; }
+ public Command RemoveItemCommand { get; set; }
+ public Command GoToNextItemCommand { get; set; }
+
+ private int _position;
+ public int Position
+ {
+ get => _position;
+ set
+ {
+ _position = value;
+ OnPropertyChanged();
+ }
+ }
+
+ private ObservableCollection _items = new();
+ public ObservableCollection Items
+ {
+ get => _items;
+ set
+ {
+ _items = value;
+ OnPropertyChanged();
+ }
+ }
+
+ public Issue19786()
+ {
+ InitializeComponent();
+
+ AddItemCommand = new Command(() =>
+ {
+ Items.Add(Items.Count.ToString());
+ });
+
+ RemoveItemCommand = new Command(() =>
+ {
+ if (Items.Count > 0)
+ Items.RemoveAt(Items.Count - 1);
+ });
+
+ GoToNextItemCommand = new Command(() =>
+ {
+ if (Position < Items.Count - 1)
+ Position++;
+ });
+
+ BindingContext = this;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs b/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs
index 4770f7c98ee0..0c9dc0db9c8d 100644
--- a/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs
+++ b/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs
@@ -128,9 +128,9 @@ public override void UpdateAdapter()
var oldItemViewAdapter = ItemsViewAdapter;
+ UnsubscribeCollectionItemsSourceChanged(ItemsViewAdapter);
if (oldItemViewAdapter != null && _initialized)
{
- UnsubscribeCollectionItemsSourceChanged(oldItemViewAdapter);
ItemsView.SetValueFromRenderer(CarouselView.PositionProperty, 0);
ItemsView.SetValueFromRenderer(CarouselView.CurrentItemProperty, null);
}
diff --git a/src/Controls/tests/UITests/Tests/Issues/Issue19786.cs b/src/Controls/tests/UITests/Tests/Issues/Issue19786.cs
new file mode 100644
index 000000000000..f3e7ab150202
--- /dev/null
+++ b/src/Controls/tests/UITests/Tests/Issues/Issue19786.cs
@@ -0,0 +1,31 @@
+using NUnit.Framework;
+using UITest.Appium;
+using UITest.Core;
+
+namespace Microsoft.Maui.AppiumTests.Issues
+{
+ public class Issue19786 : _IssuesUITest
+ {
+ public Issue19786(TestDevice device) : base(device)
+ {
+ }
+
+ public override string Issue => "[Android] Crash removing item from CarouselView";
+
+ [Test]
+ [Category(UITestCategories.CarouselView)]
+ public void RemovingItemsShouldNotCauseCrash()
+ {
+ _ = App.WaitForElement("addItemButton");
+ App.Click("addItemButton");
+ App.Click("addItemButton");
+ App.Click("addItemButton");
+ App.Click("goToNextItemButton");
+ App.Click("goToNextItemButton");
+ App.Click("removeLastItemButton");
+ App.Click("removeLastItemButton");
+ App.Click("removeLastItemButton");
+ }
+
+ }
+}