Skip to content

Commit a8a818e

Browse files
authored
[Android] -Picker dialog causes crash when page is popped while dialo… (#31747)
* [Android] -Picker dialog causes crash when page is popped while dialog is open * removed the dispose call. * Revert "removed the dispose call." This reverts commit e1e7a9f. * removed the dispose call. * Modified with Dismiss() method.
1 parent 37aef27 commit a8a818e

File tree

5 files changed

+142
-3
lines changed

5 files changed

+142
-3
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
namespace Maui.Controls.Sample.Issues
2+
{
3+
[Issue(IssueTracker.Github, 31731, "Picker dialog causes crash when page is popped while dialog is open", PlatformAffected.Android)]
4+
public class Issue31731 : NavigationPage
5+
{
6+
public Issue31731() : base(new MainPage())
7+
{
8+
}
9+
10+
public class MainPage : ContentPage
11+
{
12+
public MainPage()
13+
{
14+
var button = new Button
15+
{
16+
Text = "Navigate to Picker Page",
17+
AutomationId = "navigateButton"
18+
};
19+
20+
button.Clicked += OnNavigateClicked;
21+
22+
var statusLabel = new Label
23+
{
24+
Text = "Status: Ready",
25+
AutomationId = "statusLabel"
26+
};
27+
28+
Content = new StackLayout
29+
{
30+
Padding = new Thickness(20),
31+
Children = { statusLabel, button }
32+
};
33+
}
34+
35+
private void OnNavigateClicked(object sender, EventArgs e)
36+
{
37+
Navigation.PushAsync(new PickerPage());
38+
}
39+
}
40+
41+
public class PickerPage : ContentPage
42+
{
43+
public PickerPage()
44+
{
45+
var picker = new Picker
46+
{
47+
Title = "Select a color",
48+
ItemsSource = new List<string> { "Red", "Green", "Blue", "Yellow", "Purple" },
49+
AutomationId = "colorPicker"
50+
};
51+
52+
var instructionsLabel = new Label
53+
{
54+
Text = "Tap the picker to open the dialog, then wait for auto navigation back (3 seconds). The app should not crash.",
55+
AutomationId = "instructionsLabel",
56+
Margin = new Thickness(0, 0, 0, 20)
57+
};
58+
59+
var statusLabel = new Label
60+
{
61+
Text = "Status: Page loaded",
62+
AutomationId = "pageStatusLabel"
63+
};
64+
65+
Content = new StackLayout
66+
{
67+
Padding = new Thickness(20),
68+
Children = { instructionsLabel, statusLabel, picker }
69+
};
70+
}
71+
72+
protected override void OnNavigatedTo(NavigatedToEventArgs args)
73+
{
74+
base.OnNavigatedTo(args);
75+
76+
// Simulate the scenario: navigate back after 3 seconds
77+
// This can cause a crash if the picker dialog is open
78+
_ = Task.Run(async () =>
79+
{
80+
await Task.Delay(3000);
81+
await Dispatcher.DispatchAsync(async () =>
82+
{
83+
await Navigation.PopToRootAsync();
84+
});
85+
});
86+
}
87+
}
88+
}
89+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue31731 : _IssuesUITest
8+
{
9+
public Issue31731(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "Picker dialog causes crash when page is popped while dialog is open";
14+
15+
[Test]
16+
[Category(UITestCategories.Picker)]
17+
public void PickerDialogDoesNotCrashWhenPagePoppedWhileDialogOpen()
18+
{
19+
// Wait for the main page to load
20+
App.WaitForElement("statusLabel");
21+
App.WaitForElement("navigateButton");
22+
23+
// Navigate to the picker page
24+
App.Tap("navigateButton");
25+
26+
// Wait for picker page to load
27+
App.WaitForElement("colorPicker");
28+
App.WaitForElement("pageStatusLabel");
29+
30+
// Open the picker dialog
31+
App.Tap("colorPicker");
32+
33+
// Wait for a moment to ensure dialog is open, then wait for auto navigation
34+
// The page will automatically pop after 3 seconds
35+
// If the bug exists, this would cause a crash
36+
System.Threading.Thread.Sleep(4000); // Wait longer than the 3-second delay
37+
38+
// If we reach this point without crashing, the test passes
39+
// Verify we're back on the main page
40+
App.WaitForElement("statusLabel");
41+
App.WaitForElement("navigateButton");
42+
}
43+
}

src/Core/src/Handlers/DatePicker/DatePickerHandler.Android.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ protected override void DisconnectHandler(MauiDatePicker platformView)
5252
{
5353
if (_dialog != null)
5454
{
55-
_dialog.Hide();
56-
_dialog.Dispose();
55+
_dialog.Dismiss();
5756
_dialog = null;
5857
}
5958

src/Core/src/Handlers/Picker/PickerHandler.Android.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ protected override void DisconnectHandler(MauiPicker platformView)
2828
{
2929
platformView.Click -= OnClick;
3030

31+
if (_dialog != null)
32+
{
33+
_dialog.ShowEvent -= OnDialogShown;
34+
_dialog.DismissEvent -= OnDialogDismiss;
35+
_dialog.Dismiss();
36+
_dialog = null;
37+
}
38+
3139
base.DisconnectHandler(platformView);
3240
}
3341

src/Core/src/Handlers/TimePicker/TimePickerHandler.Android.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected override void DisconnectHandler(MauiTimePicker platformView)
2626
{
2727
if (_dialog != null)
2828
{
29-
_dialog.Hide();
29+
_dialog.Dismiss();
3030
_dialog = null;
3131
}
3232
}

0 commit comments

Comments
 (0)