dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.99k stars 1.72k forks source link

Navigationstack not cleared and strange behavior #12354

Open FM1973 opened 1 year ago

FM1973 commented 1 year ago

Description

According to the docs, when using an absolute navigation, the navigationstack should be cleared. i.e. await Shell.Current.GoToAsync($"//{nameof(MainPage1)}", true);

When I navigate to a page (Page4) which contains a button which opens a "subpage" using relative navigation, then navigate from there to another page (i.e. Page1) using absolute navigation and from there go back to the page containing the subpage (Page4) you can see the previously loaded subpage and then an animation back to the "parent" page.

I know this sounds complicated, but you can try this in the sample repo.

Sometimes, when you switch between those pages, one navigation button gets disabled without any reason.

Steps to Reproduce

  1. Clone the repo
  2. Start the app
  3. Click on "Navigation"
  4. Click on "Main 4"
  5. Click on "Sub page 1"
  6. Click on "Sub page 2"
  7. Click on Main 1 (bottom)
  8. Click on Main 4 (bottom) and see the described behavior

To get the random "disable behavior" click on Main 4 and Sub page 1 then 2 and then click on any of the other bottom buttons (not main 4), then click "Main 4" and one of the others again. Sometimes Main 4 gets disabled. If this doesn´t happen, just try again, click on Main 4 and the sub buttons and again on any other bottom button then on Main 4. You may have to do it a couple of times. Sorry, as I stated, this is very random.

Link to public reproduction project repository

https://github.com/FM1973/RefreshGridRepo.git

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11 and up

Did you find any workaround?

No workaround for the not cleared stack. To prevent the strange disable behavior one can use PopToRootAsync before navigating, if the stack contains more than one element.

var stackCount = Shell.Current.Navigation.NavigationStack.Count;
            if (stackCount > 1)
            {
                if(DeviceInfo.Platform == DevicePlatform.Android)
                    await Shell.Current.Navigation.PopToRootAsync(true);
            }

Relevant log output

No response

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

FM1973 commented 1 year ago

Any news on this one?

XamlTest commented 1 year ago

Verified this on Visual Studio Enterprise 17.7.0 Preview 1.0. Repro on Android 13.0-API33 with below Project: RefreshGridRepo.zip

Follow the repro steps, repro on the second time. image

nicop85 commented 9 months ago

I've found a similar problem to the navigation one expressed in the first part of this issue. When you navigate to sub pages from a main menu item in the shell, then change to a different main menu item and back to the first one, the subpage will still be there. The navigation stack doesn't get cleaned when you navigate to another main menu item of the shell.

I'm using VS 17.8.0, Net 8.0 and Net MAUI 8.0.3

The work around I'm temporarily using is to override the OnNavigating method of the Shell as follows:

protected async override void OnNavigating(ShellNavigatingEventArgs args)
{
    base.OnNavigating(args);

    if (args.Source.Equals(ShellNavigationSource.ShellItemChanged))
    {
        var token = args.GetDeferral();

        if (Shell.Current?.Navigation?.NavigationStack?.Count > 1)
        {
            await Shell.Current.Navigation.PopToRootAsync(false);
        }

        token?.Complete();
    }
}

This way, only when it's a ShellItemChanged (user selecting some menu item from the shell), I'm checking if there are some other subpages loaded in the stack and then I popToRoot before completing the navigation to the new main menu option.

Just in case it helps others with a similar problem.