Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue31731.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 31731, "Picker dialog causes crash when page is popped while dialog is open", PlatformAffected.Android)]
public class Issue31731 : NavigationPage
{
public Issue31731() : base(new MainPage())
{
}

public class MainPage : ContentPage
{
public MainPage()
{
var button = new Button
{
Text = "Navigate to Picker Page",
AutomationId = "navigateButton"
};

button.Clicked += OnNavigateClicked;

var statusLabel = new Label
{
Text = "Status: Ready",
AutomationId = "statusLabel"
};

Content = new StackLayout
{
Padding = new Thickness(20),
Children = { statusLabel, button }
};
}

private void OnNavigateClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new PickerPage());
}
}

public class PickerPage : ContentPage
{
public PickerPage()
{
var picker = new Picker
{
Title = "Select a color",
ItemsSource = new List<string> { "Red", "Green", "Blue", "Yellow", "Purple" },
AutomationId = "colorPicker"
};

var instructionsLabel = new Label
{
Text = "Tap the picker to open the dialog, then wait for auto navigation back (3 seconds). The app should not crash.",
AutomationId = "instructionsLabel",
Margin = new Thickness(0, 0, 0, 20)
};

var statusLabel = new Label
{
Text = "Status: Page loaded",
AutomationId = "pageStatusLabel"
};

Content = new StackLayout
{
Padding = new Thickness(20),
Children = { instructionsLabel, statusLabel, picker }
};
}

protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);

// Simulate the scenario: navigate back after 3 seconds
// This can cause a crash if the picker dialog is open
_ = Task.Run(async () =>
{
await Task.Delay(3000);
await Dispatcher.DispatchAsync(async () =>
{
await Navigation.PopToRootAsync();
});
});
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue31731 : _IssuesUITest
{
public Issue31731(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "Picker dialog causes crash when page is popped while dialog is open";

[Test]
[Category(UITestCategories.Picker)]
public void PickerDialogDoesNotCrashWhenPagePoppedWhileDialogOpen()
{
// Wait for the main page to load
App.WaitForElement("statusLabel");
App.WaitForElement("navigateButton");

// Navigate to the picker page
App.Tap("navigateButton");

// Wait for picker page to load
App.WaitForElement("colorPicker");
App.WaitForElement("pageStatusLabel");

// Open the picker dialog
App.Tap("colorPicker");

// Wait for a moment to ensure dialog is open, then wait for auto navigation
// The page will automatically pop after 3 seconds
// If the bug exists, this would cause a crash
System.Threading.Thread.Sleep(4000); // Wait longer than the 3-second delay

// If we reach this point without crashing, the test passes
// Verify we're back on the main page
App.WaitForElement("statusLabel");
App.WaitForElement("navigateButton");
}
}
9 changes: 9 additions & 0 deletions src/Core/src/Handlers/Picker/PickerHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ protected override void DisconnectHandler(MauiPicker platformView)
{
platformView.Click -= OnClick;

if (_dialog != null)
{
_dialog.ShowEvent -= OnDialogShown;
_dialog.DismissEvent -= OnDialogDismiss;
_dialog.Hide();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sheiksyedm why is this hide vs dismiss?

Copy link
Contributor Author

@sheiksyedm sheiksyedm Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PureWeen Dismiss() is the proper approach to completely clean up the dialog, whereas Hide() only hides the dialog. I have now updated the code to use Dismiss() instead of Hide(). Previously, I had referred to Hide() from the other picker classes. I have now completed the changes to use Dismiss() in those pickers as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide() makes the dialog invisible but may not fully clean up resources
Dismiss() properly dismisses the dialog and triggers cleanup

In this case, Dismiss have more sense.

_dialog.Dispose();
_dialog = null;
}

base.DisconnectHandler(platformView);
}

Expand Down