Skip to content

NavHost cannot be null on Back key pressed #15102

@luwed

Description

@luwed

Description

I noticed InvalidOperationException (NavHost cannot be null) when running app with debugger on Android 12 on Back key pressed.

Steps to Reproduce

  1. Create Maui app
  2. Define MainPage in App.xaml.cs as:
var rootPage = new MainPage();
MainPage = new NavigationPage(rootPage);
  1. Run app with debugger on Android 12
  2. Move app to background using Back key
  3. Return to the app using main launcher (icon)
  4. Press Back key again

Actual result:
InvalidOperationException is thrown:

NavHost cannot be null

   at Microsoft.Maui.Platform.StackNavigationManager.get_NavHost() in D:\a\_work\1\s\src\Core\src\Platform\Android\Navigation\StackNavigationManager.cs:line 32
   at Microsoft.Maui.Platform.NavigationViewFragment.OnResume() in D:\a\_work\1\s\src\Core\src\Platform\Android\Navigation\NavigationViewFragment.cs:line 75
   at AndroidX.Fragment.App.Fragment.n_OnResume(IntPtr jnienv, IntPtr native__this) in /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.Fragment.cs:line 2614
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 22

Expected result:
No exception

Link to public reproduction project repository

https://github.com/luwed/maui-stuff/tree/NavHost-cannot-be-null-on-back

Version with bug

Unknown/Other (please specify)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12

Did you find any workaround?

No response

Relevant log output

No response

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions