Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App.Current.PageDisappearing is not raised when a page is popped from the navigation stack. #14092

Open
Keflon opened this issue Mar 21, 2023 · 5 comments
Labels
area-navigation NavigationPage platform/android 🤖 platform/iOS 🍎 platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@Keflon
Copy link

Keflon commented Mar 21, 2023

Description

App.Current.PageDisappearing is not raised when a page is popped from the navigation stack.

Steps to Reproduce

  1. Create a File->New MAUI app.
  2. Replace the constructor in App.xaml.cs with this:
public partial class App : Application
{
	public App()
	{
		InitializeComponent();
		//MainPage = new AppShell();
		MainPage = new NavigationPage(new MainPage());

        App.Current.PageAppearing += Current_PageAppearing;
        App.Current.PageDisappearing += Current_PageDisappearing;
	}

    private void Current_PageDisappearing(object sender, Page e)
    {
        Debug.WriteLine($"Current_PageDisappearing {e}");
    }

    private void Current_PageAppearing(object sender, Page e)
    {
        Debug.WriteLine($"Current_PageAppearing {e}");
    }
}
  1. Replace the click-handler in MainPage.xaml.cs with this:
    private void OnCounterClicked(object sender, EventArgs e)
    {
        App.Current.MainPage.Navigation.PushAsync(new ContentPage { Title="Child Page"});
    }
  1. Run the app.
  2. Look at the debug output from the new Current_PageDisappearing and Current_PageAppearing event handlers.
  3. Observe that MainPage and NavigationPage are reported as appearing.
[0:] Current_PageAppearing MauiPopNavigationBug.MainPage
[0:] Current_PageAppearing Microsoft.Maui.Controls.NavigationPage
  1. Tap the button.
  2. Observe MainPage disappears and ContentPage appears.
[0:] Current_PageDisappearing MauiPopNavigationBug.MainPage
[0:] Current_PageAppearing Microsoft.Maui.Controls.ContentPage
  1. Tap the back button
  2. [BUG] Observe the ContentPage is popped, the MainPage appearing event is raised but the ContentPage PageDisappearing event is not raised.
[0:] Current_PageAppearing MauiPopNavigationBug.MainPage

Link to public reproduction project repository

https://github.com/Keflon/MauiPopNavigationBug

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, I was not able test on other platforms

Affected platform versions

All current / latest.

Did you find any workaround?

Subscribe to the following events:

App.Current.DescendantAdded += CurrentApplication_DescendantAdded;
App.Current.DescendantRemoved += CurrentApplication_DescendantRemoved;

Do something like this:

private void CurrentApplication_DescendantAdded(object sender, ElementEventArgs e)
{
    if (e.Element is Page cp)
    {
        cp.Disappearing += PageDisappearing;
        cp.Appearing += PageAppearing;
    }
}

private async void CurrentApplication_DescendantRemoved(object sender, ElementEventArgs e)
{
    if (e.Element is Page cp)
    {
        cp.Appearing -= PageAppearing;

        await Task.Yield();     // Reason: The disappearing event was/is raised after the DescendantRemoved event.
        cp.Disappearing -= PageDisappearing;
    }
}

The PageAppearing and PageDisappearing events are correctly raised.

The above is not tested, however it is a subset of working CurrentApplication_DescendantAdded and CurrentApplication_DescendantRemoved implementations found here: https://github.com/Keflon/Maui.MvvmZero/blob/eada8a7059fadc248de13456b3be627373384bd8/Maui.MvvmZero/Implementation/PageServiceZero.cs

Relevant log output

[0:] Current_PageAppearing MauiPopNavigationBug.*
[0:] Current_PageDisappearing MauiPopNavigationBug.*
@Keflon Keflon added the t/bug Something isn't working label Mar 21, 2023
@drasticactions
Copy link
Contributor

Can you try this on Xamarin.Forms and report what it does? Is this a regression from it or does it do the same thing?

@Keflon
Copy link
Author

Keflon commented Mar 22, 2023

Xamarin.Forms is fine for UWP, iOS and Android.
Repo here: https://github.com/Keflon/XamarinPopNavigationTest/tree/master
I tested Debug builds only.

@rachelkang rachelkang added the area-navigation NavigationPage label Mar 22, 2023
@rachelkang
Copy link
Member

@PureWeen does this issue look familiar / in the domain of what you've been working on recently?

@rachelkang rachelkang added this to the Backlog milestone Mar 22, 2023
@ghost
Copy link

ghost commented Mar 22, 2023

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@Zhanglirong-Winnie Zhanglirong-Winnie added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jun 19, 2023
@Zhanglirong-Winnie
Copy link

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 1.0. Can repro on windows platform with sample project.
MauiPop.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-navigation NavigationPage platform/android 🤖 platform/iOS 🍎 platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants