dotnet / dotnet/maui

System.InvalidOperationException: NavigationManager cannot be null here

Open
#28,075 11 comments 0 reactions 0 assignees View on GitHub
area-controls-shell area-navigation platform/android t/bug
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

I noticed that an InvalidOperationException would be thrown when pushing a `NavigationPage` as a modal and pressing the Android back button

```
System.InvalidOperationException: NavigationManager cannot be null here
at Microsoft.Maui.Platform.NavigationViewFragment.get_NavigationManager() in D:\a\_work\1\s\src\Core\src\Platform\Android\Navigation\NavigationViewFragment.cs:line 25
at Microsoft.Maui.Platform.NavigationViewFragment.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\Navigation\NavigationViewFragment.cs:line 56
at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState) in C:\a\_work\1\s\generated\androidx.fragment.fragment\obj\Release
et6.0-android\generated\src\AndroidX.Fragment.App.Fragment.cs:line 2031
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 363
```

### Steps to Reproduce

1. Open the repo project on Android
2. Press the Navigate button
3. Quickly press the back button

Expected outcome:
No crash should occur

Actual outcome:
App will crash with InvalidOperationException

### Link to public reproduction project repository

https://github.com/WillMDK/navigationmanagernull

### Version with bug

8.0.91 SR9.1

### Is this a regression from previous behavior?

Not sure, did not test other versions

### Last version that worked well

Unknown/Other

### Affected platforms

Android

### Affected platform versions

Android 14

### Did you find any workaround?

_No response_

### Relevant log output

```shell

```

Contributor guide

Open the contributing guide

Research direction

Start in src/Core/src/Platform/Android/Navigation/NavigationViewFragment.cs, especially get_NavigationManager and OnCreateView, then run the linked navigationmanagernull reproduction on Android 14. Reproduce the quick Navigate-then-back sequence and trace the fragment lifecycle; done means pressing Back during navigation no longer crashes with InvalidOperationException.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, csharp
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.