Skip to content

Commit 7291b48

Browse files
NirmalKumarYuvarajKarthikRajaKalaimaniPureWeen
authored
Fixed Net 8: Page and control Unloaded events firing on iOS when navigating to another page (#25852)
* Fix included and UI test case added * Snapshot added for windows platform. * Test case modified and committed * Comments added for my fix. * comments modified * - fix up test and extension a bit --------- Co-authored-by: Karthik Raja <karthikraja.kalaimani@syncfusion.com> Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
1 parent ff62205 commit 7291b48

File tree

5 files changed

+106
-2
lines changed

5 files changed

+106
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace Maui.Controls.Sample.Issues
2+
{
3+
[Issue(IssueTracker.Github, 21916, "Page and control Unloaded events firing on iOS when navigating to another page", PlatformAffected.iOS)]
4+
public class Issue21916 : Shell
5+
{
6+
public Issue21916()
7+
{
8+
CurrentItem = new Issue21916_MainPage();
9+
}
10+
11+
public class Issue21916_MainPage : ContentPage
12+
{
13+
int mainPageLoadedCount = 0;
14+
int mainPageUnloadedCount = 0;
15+
Label label;
16+
bool isNavigated;
17+
public Issue21916_MainPage()
18+
{
19+
var stack = new StackLayout();
20+
label = new Label();
21+
var Button = new Button();
22+
Button.Text = "Click to navigate new page";
23+
Button.AutomationId = "Button";
24+
Button.Clicked += Clicked;
25+
stack.Children.Add(Button);
26+
stack.Children.Add(label);
27+
this.Content = stack;
28+
this.Loaded += MainPage_Loaded;
29+
this.Unloaded += MainPage_UnLoaded;
30+
}
31+
32+
private async void Clicked(object sender, EventArgs e)
33+
{
34+
isNavigated = true;
35+
await Navigation.PushAsync(new Issue21916_NewPage());
36+
}
37+
38+
private void MainPage_UnLoaded(object sender, EventArgs e)
39+
{
40+
mainPageUnloadedCount++;
41+
}
42+
43+
private void MainPage_Loaded(object sender, EventArgs e)
44+
{
45+
if (isNavigated)
46+
{
47+
mainPageLoadedCount++;
48+
label.Text = $"Unloaded event triggered {mainPageUnloadedCount} times\nLoaded event triggered {mainPageLoadedCount} times";
49+
}
50+
}
51+
}
52+
53+
public class Issue21916_NewPage : ContentPage
54+
{
55+
public Issue21916_NewPage()
56+
{
57+
var stack = new StackLayout();
58+
var Button = new Button();
59+
Button.Text = "Click to navigate main page";
60+
Button.AutomationId = "Button";
61+
Button.Clicked += Clicked;
62+
stack.Children.Add(Button);
63+
this.Content = stack;
64+
}
65+
66+
private void Clicked(object sender, EventArgs e)
67+
{
68+
Shell.Current.GoToAsync("..");
69+
}
70+
}
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#if !ANDROID && !MACCATALYST
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues
7+
{
8+
public class Issue21916 : _IssuesUITest
9+
{
10+
public Issue21916(TestDevice testDevice) : base(testDevice)
11+
{
12+
}
13+
14+
public override string Issue => "Page and control Unloaded events firing on iOS when navigating to another page";
15+
16+
[Test]
17+
[Category(UITestCategories.Shell)]
18+
public void Shell_Issue21916()
19+
{
20+
App.WaitForElement("Button");
21+
App.Click("Button");
22+
App.WaitForElement("Button");
23+
App.Click("Button");
24+
VerifyScreenshot();
25+
}
26+
}
27+
}
28+
#endif
Loading
Loading

src/Core/src/Platform/iOS/ViewExtensions.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,9 @@ internal static IDisposable OnLoaded(this UIView uiView, Action action)
648648

649649
void OnLifeCycleEventsMovedToWindow(object? sender, EventArgs e)
650650
{
651-
OnLoadedCheck(null);
651+
//The MovedToWindow fires multiple times during navigation animations, causing repeated OnLoadedCheck calls.
652+
//BeginInvokeOnMainThread ensures OnLoadedCheck executes after all window transitions are complete.
653+
uiView.BeginInvokeOnMainThread(() => OnLoadedCheck(null));
652654
}
653655
}
654656
else
@@ -728,7 +730,9 @@ internal static IDisposable OnUnloaded(this UIView uiView, Action action)
728730

729731
void OnLifeCycleEventsMovedToWindow(object? sender, EventArgs e)
730732
{
731-
UnLoadedCheck();
733+
//The MovedToWindow fires multiple times during navigation animations, causing repeated UnLoadedCheck calls.
734+
//BeginInvokeOnMainThread ensures UnLoadedCheck executes after all window transitions are complete.
735+
uiView.BeginInvokeOnMainThread(UnLoadedCheck);
732736
}
733737
}
734738

0 commit comments

Comments
 (0)