diff --git a/src/Controls/src/Core/NavigationPage/NavigationPage.Legacy.cs b/src/Controls/src/Core/NavigationPage/NavigationPage.Legacy.cs index 2262c4ed7eab..a2d7ca063ec4 100644 --- a/src/Controls/src/Core/NavigationPage/NavigationPage.Legacy.cs +++ b/src/Controls/src/Core/NavigationPage/NavigationPage.Legacy.cs @@ -43,9 +43,6 @@ async Task RemoveAsyncInner( FireDisappearing(page); - if (InternalChildren.Last() == page) - FireAppearing((Page)InternalChildren[NavigationPageController.StackDepth - 2]); - var args = new NavigationRequestedEventArgs(page, animated); var removed = true; @@ -62,8 +59,14 @@ async Task RemoveAsyncInner( if (!removed && !fast) return CurrentPage; + bool isLastPage = InternalChildren.Last() == page; RemoveFromInnerChildren(page); + if (isLastPage && NavigationPageController.StackDepth >= 2) + { + FireAppearing((Page)InternalChildren[NavigationPageController.StackDepth - 2]); + } + CurrentPage = (Page)InternalChildren.Last(); if (Popped != null) diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue28414.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue28414.cs new file mode 100644 index 000000000000..2a234c570979 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue28414.cs @@ -0,0 +1,111 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 28414, "NavigationStack updated when OnAppearing triggered", PlatformAffected.iOS)] + +public class Issue28414 : NavigationPage +{ + public Issue28414() : base(new Issue28414FirstPage()) + { + + } +} + +public class Issue28414FirstPage : ContentPage +{ + public Issue28414FirstPage() + { + Title = "FirstPage"; + + var navigateButton = new Button + { + Text = "Go to second page", + VerticalOptions = LayoutOptions.Center, + HorizontalOptions = LayoutOptions.Center, + AutomationId = "FirstPageButton" + }; + + navigateButton.Clicked += async (sender, args) => + { + await Navigation.PushAsync(new Issue28414SecondPage()); + }; + + Content = new VerticalStackLayout + { + Children = { navigateButton } + }; + } +} + +public class Issue28414SecondPage : ContentPage +{ + private Label label; + + public Issue28414SecondPage() + { + Title = "SecondPage"; + + label = new Label + { + Text = "Initial state", + AutomationId = "OnAppearingLabel" + }; + + var navigateButton = new Button + { + Text = "Go to third page", + VerticalOptions = LayoutOptions.Center, + HorizontalOptions = LayoutOptions.Center, + AutomationId = "SecondPageButton" + }; + + navigateButton.Clicked += async (sender, args) => + { + label.Text = string.Empty; + await Navigation.PushAsync(new Issue28414ThirdPage()); + }; + + Content = new VerticalStackLayout + { + Children = { label, navigateButton } + }; + } + + protected override void OnAppearing() + { + base.OnAppearing(); + if (Navigation.NavigationStack.Count == 2) + { + label.Text = $"Stack has {Navigation.NavigationStack.Count} pages"; + } + else + { + label.Text = "Page not popped yet"; + } + } +} + +public class Issue28414ThirdPage : ContentPage +{ + public Issue28414ThirdPage() + { + Title = "ThirdPage"; + + var button = new Button + { + Text = "Go Back", + HorizontalOptions = LayoutOptions.Center, + VerticalOptions = LayoutOptions.Center, + AutomationId = "ThirdPageButton" + }; + + button.Clicked += async (sender, args) => + { + await Navigation.PopAsync(); + }; + + Content = new VerticalStackLayout + { + Children = { button } + }; + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28414.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28414.cs new file mode 100644 index 000000000000..cb9d93028926 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28414.cs @@ -0,0 +1,27 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue28414 : _IssuesUITest +{ + public Issue28414(TestDevice testDevice) : base(testDevice) + { + } + public override string Issue => "NavigationStack updated when OnAppearing triggered"; + + [Test] + [Category(UITestCategories.Navigation)] + public void NavigationStackUpdatesOnPop() + { + App.WaitForElement("FirstPageButton"); + App.Tap("FirstPageButton"); + App.WaitForElement("SecondPageButton"); + App.Tap("SecondPageButton"); + App.WaitForElement("ThirdPageButton"); + App.Tap("ThirdPageButton"); + var label = App.WaitForElement("OnAppearingLabel"); + Assert.That(label.GetText(), Is.EqualTo("Stack has 2 pages")); + } +} \ No newline at end of file