Skip to content

Commit 3990526

Browse files
Vignesh-SF3580PureWeen
authored andcommitted
[Android,Windows] Fix NavigatingFrom event order inconsistency with PushAsync (#31536)
* Update NavigationPage.cs * Added test * Update NavigationPage.cs * Update NavigationPage.cs
1 parent 1926e1a commit 3990526

File tree

3 files changed

+149
-1
lines changed

3 files changed

+149
-1
lines changed

src/Controls/src/Core/NavigationPage/NavigationPage.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,11 +869,13 @@ protected override Task OnPushAsync(Page root, bool animated)
869869
return Owner.SendHandlerUpdateAsync(animated,
870870
() =>
871871
{
872+
// Move the SendNavigating here so that it's fired prior to the stack being modified
873+
// This ensures consistent event ordering across all platforms (iOS, Catalyst, Android, Windows)
874+
Owner.SendNavigating(previousPage);
872875
Owner.PushPage(root);
873876
},
874877
() =>
875878
{
876-
Owner.SendNavigating(previousPage);
877879
Owner.FireDisappearing(previousPage);
878880
Owner.FireAppearing(root);
879881
},
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 31520, "NavigatingFrom is triggered first when using PushAsync", PlatformAffected.Android)]
4+
public class Issue31520 : NavigationPage
5+
{
6+
public static int count = 0;
7+
public static Label statusLabel;
8+
public static Issue31520Page2 currentPage2;
9+
10+
public Issue31520() : base(new Issue31520Page1())
11+
{
12+
13+
}
14+
}
15+
16+
public class Issue31520Page1 : ContentPage
17+
{
18+
public Issue31520Page1()
19+
{
20+
SetupPageContent();
21+
22+
Disappearing += (s, e) =>
23+
{
24+
Issue31520.count++;
25+
};
26+
27+
NavigatingFrom += (s, e) =>
28+
{
29+
if (Issue31520.statusLabel is not null)
30+
{
31+
Issue31520.statusLabel.Text = (Issue31520.count == 0)
32+
? "NavigatingFrom triggered before Disappearing"
33+
: "NavigatingFrom triggered after Disappearing";
34+
35+
Issue31520.currentPage2?.UpdateNavigatingStatusLabel();
36+
}
37+
};
38+
}
39+
40+
private void SetupPageContent()
41+
{
42+
Title = "Page 1";
43+
44+
Issue31520.statusLabel = new Label
45+
{
46+
Text = "Ready",
47+
AutomationId = "statusLabel"
48+
};
49+
50+
var button = new Button
51+
{
52+
Text = "Push",
53+
AutomationId = "PushButton"
54+
};
55+
button.Clicked += OnCounterClicked;
56+
57+
Content = new VerticalStackLayout
58+
{
59+
Padding = new Thickness(30, 0),
60+
Spacing = 25,
61+
Children =
62+
{
63+
Issue31520.statusLabel,
64+
button
65+
}
66+
};
67+
}
68+
69+
private void OnCounterClicked(object sender, EventArgs e)
70+
{
71+
Navigation.PushAsync(new Issue31520Page2());
72+
}
73+
}
74+
75+
public class Issue31520Page2 : ContentPage
76+
{
77+
private Label navigatingStatusLabel;
78+
79+
public Issue31520Page2()
80+
{
81+
Issue31520.currentPage2 = this;
82+
83+
Appearing += (s, e) =>
84+
{
85+
Issue31520.count++;
86+
};
87+
88+
Title = "Page 2";
89+
90+
var button = new Button
91+
{
92+
Text = "Pop",
93+
AutomationId = "ChildPageButton"
94+
};
95+
button.Clicked += OnButtonClicked;
96+
97+
navigatingStatusLabel = new Label
98+
{
99+
Text = Issue31520.statusLabel?.Text ?? "Ready",
100+
AutomationId = "NavigatingStatusLabel"
101+
};
102+
103+
Content = new StackLayout
104+
{
105+
Children =
106+
{
107+
navigatingStatusLabel,
108+
button
109+
}
110+
};
111+
}
112+
113+
public void UpdateNavigatingStatusLabel()
114+
{
115+
if (navigatingStatusLabel is not null && Issue31520.statusLabel is not null)
116+
navigatingStatusLabel.Text = Issue31520.statusLabel.Text;
117+
}
118+
119+
private void OnButtonClicked(object sender, EventArgs e)
120+
{
121+
Navigation.PopAsync();
122+
}
123+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue31520 : _IssuesUITest
8+
{
9+
public Issue31520(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
public override string Issue => "NavigatingFrom is triggered first when using PushAsync";
13+
14+
[Test]
15+
[Category(UITestCategories.Navigation)]
16+
public void NavigatingFromTriggeredFirst()
17+
{
18+
App.WaitForElement("PushButton");
19+
App.Tap("PushButton");
20+
var label = App.WaitForElement("NavigatingStatusLabel");
21+
Assert.That(label.GetText(), Is.EqualTo("NavigatingFrom triggered before Disappearing"));
22+
}
23+
}

0 commit comments

Comments
 (0)