question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash during navigation with Navigation page

See original GitHub issue

Description

https://github.com/tmijieux/MauiNavigationBug/blob/6b8cd9283ecdcd2bc6e2b0718fd8cccb3f35f98f/App.xaml.cs#L5-L32 I try to do the following navigation pattern in my application (very common in my application)

  • add a new page at the bottom of the navigation stack
  • pop to root

The aim is to go to a new page while popping everything that was currently on the stack. The following code throws 2 exceptions :

  • the first one is catched by the catch block
  • there is another one that is not catched (like if it was on another thread) and crash the application, (there is no absolutely no information about the stack trace on that second exception … and I do not know why …) (EDIT i found out about that second exception see my next answers)
namespace MauiApp1;

public partial class App : Application
{
    private NavigationPage _nav;
    public App()
    {
        InitializeComponent();
        _nav = new NavigationPage(new MainPage());
        MainPage = _nav;
    }

    public async void Navigate()
    {
        try
        {
            await NavigateImpl();
        }
        catch (Exception ex)
        {
            Debug.WriteLine($"ex={ex}");
        }
    }

    private async Task NavigateImpl()
    {
        var page = new OtherPage();
        _nav.Navigation.InsertPageBefore(page, _nav.RootPage);
        await _nav.PopToRootAsync(false);
    }
}

Steps to Reproduce

  1. clone https://github.com/tmijieux/MauiNavigationBug/commit/6b8cd9283ecdcd2bc6e2b0718fd8cccb3f35f98f
  2. launch on android
  3. tap the “navigate” button

Version with bug

Preview 12 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11 (pixel 5 emulator)

Did you find any workaround?

not yet

Relevant log output

First exception:

[0:] ex=System.InvalidOperationException: Previous Navigation Request is still Processing
   at Microsoft.Maui.Platform.StackNavigationManager.ApplyNavigationRequest(NavigationRequest args) in D:\a\1\s\src\Core\src\Platform\Android\Navigation\StackNavigationManager.cs:line 87
   at Microsoft.Maui.Platform.StackNavigationManager.RequestNavigation(NavigationRequest e) in D:\a\1\s\src\Core\src\Platform\Android\Navigation\StackNavigationManager.cs:line 321
   at Microsoft.Maui.Handlers.NavigationViewHandler.RequestNavigation(NavigationViewHandler arg1, INavigationView arg2, Object arg3) in D:\a\1\s\src\Core\src\Handlers\NavigationPage\NavigationViewHandler.Android.cs:line 46
   at Microsoft.Maui.CommandMapper`2.<>c__DisplayClass6_0[[Microsoft.Maui.INavigationView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.NavigationViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v, Object o) in D:\a\1\s\src\Core\src\CommandMapper.cs:line 89
   at Microsoft.Maui.CommandMapper.InvokeCore(String key, IElementHandler viewHandler, IElement virtualView, Object args) in D:\a\1\s\src\Core\src\CommandMapper.cs:line 30
   at Microsoft.Maui.CommandMapper.Invoke(IElementHandler viewHandler, IElement virtualView, String property, Object args) in D:\a\1\s\src\Core\src\CommandMapper.cs:line 48
   at Microsoft.Maui.Handlers.ElementHandler.Invoke(String command, Object args) in D:\a\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 90
   at Microsoft.Maui.Controls.NavigationPage.Microsoft.Maui.INavigationView.RequestNavigation(NavigationRequest eventArgs) in D:\a\1\s\src\Controls\src\Core\HandlerImpl\NavigationPage\NavigationPage.Impl.cs:line 59
   at Microsoft.Maui.Controls.NavigationPage.SendHandlerUpdateAsync(Boolean animated, Action processStackChanges, Action firePostNavigatingEvents, Action fireNavigatedEvents) in D:\a\1\s\src\Controls\src\Core\HandlerImpl\NavigationPage\NavigationPage.Impl.cs:line 264
   at Microsoft.Maui.Controls.NavigationPage.PopToRootAsync(Boolean animated) in D:\a\1\s\src\Controls\src\Core\NavigationPage.cs:line 238
   at MauiApp1.App.NavigateImpl() in C:\src\MauiApp1\App.xaml.cs:line 31
   at MauiApp1.App.Navigate() in C:\src\MauiApp1\App.xaml.cs:line 19


2nd exception (uncatchable somehow…)

**System.ArgumentOutOfRangeException:** 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')'

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
tmijieuxcommented, Jan 30, 2022

I think i understand the problem https://github.com/dotnet/maui/blob/2c5ada839f4818272d3ae47f9d3b7547c94a347c/src/Controls/src/Core/HandlerImpl/NavigationPage/NavigationPage.Impl.cs#L357-L359

https://github.com/dotnet/maui/blob/2c5ada839f4818272d3ae47f9d3b7547c94a347c/src/Controls/src/Core/HandlerImpl/NavigationPage/NavigationPage.Impl.cs#L199-L202

The call to SendHandlerUpdate here, is a “fire and forget” of the async void method because it assumes (as said in the comment) that the InsertPageBefore and RemovePage methods will not update the current view and that the implementation will actually not transmit navigation information to native stack.

But actually if you are on RootPage and insert a page just below root page you need to make the backbutton appear and the reverse is also true, if there is two page in the stack and remove the one below to top , you will need to make the backbutton disappear, so the assumption that this methods needs not update the view is false.

By example, I think this was assumed the handler would use some kind of “fast exit” path like this one. https://github.com/dotnet/maui/blob/2c5ada839f4818272d3ae47f9d3b7547c94a347c/src/Core/src/Platform/Android/Navigation/StackNavigationManager.cs#L115-L126

I just setup my sample alongside existing samples in the maui repository to be able to have breakpoints in the maui code, and it actually get past this condition (it is false, so the code does not return here) so I think past this point the “native” stack will be updated.

I was also able to catch the OutOfRange exception in the debug session when building mauicode because of VS breaking when throwing exception and it is happening here: https://github.com/dotnet/maui/blob/2c5ada839f4818272d3ae47f9d3b7547c94a347c/src/Controls/src/Core/HandlerImpl/NavigationPage/NavigationPage.Impl.cs#L83-L109

line 91:

  • newStack is of length 2
  • InternalChildren is only of length 1
  • i=1 (second item)

image

0reactions
J-Swiftcommented, Mar 17, 2022

@tmijieux did you submit a PR for your changes? I really think the API needs to be updated to be await-able. Honestly, the more I think about it, the weirder it is to me that they aren’t already.

Read more comments on GitHub >

github_iconTop Results From Across the Web

App is crashing when I try to navigate between screens
My App is crashing with no error logs on the console when I'm trying to navigate between screens based on the onAuthStateChanged() method....
Read more >
Stop Android Navigation Crashes
Our testers showed us how to reproduce a navigation crash by double taping very fast on a button responsible to navigate somewhere.
Read more >
Application crashes when navigating twice through the same ...
When attempting to navigate through a nested graph more than once the application ... P3, Crash caused by navigating twice via the same...
Read more >
Xamarin.Forms - Crash with Navigation.RemovePage
Hey! I had some issues with the use of Navigation in my Xamarin.Forms app on a barcode scanner page, which was luckily solved...
Read more >
How to fix Maps when it crashes - Android
If the Google Maps app on your phone or tablet crashes or has other issues that affect your experience ... Find "Map -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found