adospace / reactorui-maui

MauiReactor is a MVU UI framework built on top of .NET MAUI
MIT License
588 stars 49 forks source link

OnWillUnmount not called with NavigationPage / ShellPage #164

Closed awasilik closed 11 months ago

awasilik commented 11 months ago

Hey,

I'm playing around with the framework and I noticed that OnWillUnmount is not called by the framework when going back from Page using Back button using

Disclaimer: I have tested it with both Navigation and Shell. Below there is and example how it doesn't not work in Navigation and Shell approach, this is not a code I have in my app.

Quick sandbox code

  1. Open app
  2. Open other page
  3. Press back button

Edit - for Navigation there is a workaround, but for Shell there is none.

public class MainNavigationPage : Component
{
    private bool isShellNavigation = false;

    public override VisualNode Render()
    {
          new MauiReactor.Shell()
          {
              new HomePage()
          }
    }
}

public class HomePage : Component
{
    protected override void OnMounted()
    {
        base.OnMounted();

        Routing.RegisterRoute<OtherPage>("otherpage");
    }

    public override VisualNode Render() =>
        new ContentPage()
        {
            new VStack()
            {
                new Button()
                    .Text("Go to other page (Shell)")
                    .OnClicked(() => Shell.Current.GoToAsync("otherpage"))
            }
        };
}

public class OtherPage : Component
{
    protected override void OnMounted()
    {
        base.OnMounted();

        // This is properly called on navigation with Shell and Navigation
    }

    protected override void OnWillUnmount()
    {
        base.OnWillUnmount();

        // This is never called
    }

    // Open the page and press back button
    public override VisualNode Render() =>
        new ContentPage();
}
juanyacovino77 commented 11 months ago

NavigationPage and Shell are two different navigations systems and cannot be use mixed like you are trying here

Please read this page to be clear: https://adospace.gitbook.io/mauireactor/components/navigation

awasilik commented 11 months ago

This is not what I wanted to show. I just wanted to show that in both examples this doesn't work. This is just a sandbox and of course I am not mixing those approaches in my app

Edited

adospace commented 11 months ago

Hi, thank you for reporting this. I've fixed the problem with the NavigationPage. Regarding the Shell navigation system, I'm afraid that the OnWillOnmount() can't be called on the component creating the page because it's directly handled by the shell.

The workaround is to attach/detach the Appearing event:

return new ContentPage("Home")
{
    new Label("Home")
        .VCenter()
        .HCenter()
}
.OnAppearing(()=> System.Diagnostics.Debug.WriteLine("HomePage.OnAppearing()"))
.OnDisappearing(() => System.Diagnostics.Debug.WriteLine("HomePage.OnDisappearing()"));