{"record":{"id":"8cc107f5edfd17c8","repo":"dotnet/maui","slug":"insertpagebefore-is-not-supported-globally-on-andr","errorCode":null,"errorMessage":"InsertPageBefore is not supported globally on Android, please use a NavigationPage.","messagePattern":"InsertPageBefore is not supported globally on Android, please use a NavigationPage\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/AppCompat/Platform.cs","lineNumber":115,"sourceCode":"\t\t{\n\t\t\tif (_disposed)\n\t\t\t\treturn;\n\t\t\t_disposed = true;\n\n\t\t\tFormsAppCompatActivity.BackPressed -= HandleBackPressed;\n\n\t\t\tSetPage(null);\n\n\t\t\tvar activity = _context?.GetActivity();\n\t\t\tif (_embedded && activity != null)\n\t\t\t{\n\t\t\t\tPopupManager.Unsubscribe(activity);\n\t\t\t}\n\t\t}\n\n\t\tvoid INavigation.InsertPageBefore(Page page, Page before)\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"InsertPageBefore is not supported globally on Android, please use a NavigationPage.\");\n\t\t}\n\n\t\tIReadOnlyList<Page> INavigation.ModalStack => _navModel.Modals.ToList();\n\n\t\tIReadOnlyList<Page> INavigation.NavigationStack => new List<Page>();\n\n\t\tTask<Page> INavigation.PopAsync()\n\t\t{\n\t\t\treturn ((INavigation)this).PopAsync(true);\n\t\t}\n\n\t\tTask<Page> INavigation.PopAsync(bool animated)\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"PopAsync is not supported globally on Android, please use a NavigationPage.\");\n\t\t}\n\n\t\tTask<Page> INavigation.PopModalAsync()\n\t\t{","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/AppCompat/Platform.cs#L97-L133","documentation":"Thrown by the Platform class's explicit INavigation.InsertPageBefore implementation. On Android, the application-level (global) navigation only supports modal push/pop; page-stack operations (InsertPageBefore, PushAsync, PopAsync, PopToRootAsync, RemovePage) require a NavigationPage host. The platform's INavigation deliberately throws to direct developers to use NavigationPage for stack-based navigation.","triggerScenarios":"Calling Application.Current.MainPage.Navigation.InsertPageBefore(...) (or any page-stack INavigation method obtained from the application/platform level rather than from a NavigationPage) on Android. The global Navigation property resolves to this Platform implementation when the MainPage is not wrapped in a NavigationPage.","commonSituations":"Cross-platform code that uses the page's Navigation property without ensuring a NavigationPage is the root. iOS works because its platform navigation supports these calls; Android does not. Porting an iOS-first app to Android.","solutions":["Wrap your root page in a NavigationPage (new NavigationPage(rootPage)) so that page-stack navigation resolves to the NavigationPage's INavigation, not the platform's.","Use only PushModalAsync/PopModalAsync at the global/platform level on Android.","Refactor shared navigation code to obtain the Navigation instance from a NavigationPage, not from Application.Current.MainPage.Navigation."],"exampleFix":"// before\nApplication.Current.MainPage = new ContentPage();\nawait Application.Current.MainPage.Navigation.PushAsync(new DetailPage()); // throws on Android\n\n// after\nApplication.Current.MainPage = new NavigationPage(new ContentPage());\nawait Application.Current.MainPage.Navigation.PushAsync(new DetailPage());","handlingStrategy":"validation","validationCode":"// Ensure a NavigationPage hosts the root before using InsertPageBefore.\nstatic bool SupportsGlobalStackNavigation(Page root) => root is NavigationPage;\n// Usage:\nif (!SupportsGlobalStackNavigation(Application.Current.MainPage))\n    Application.Current.MainPage = new NavigationPage(Application.Current.MainPage);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize on NavigationPage as the root for any app using page-stack navigation on Android.","Create a shared INavigationService that throws or redirects to modals when no NavigationPage is present.","Write platform-aware unit tests that assert page-stack operations are only invoked under a NavigationPage."],"tags":["android","navigation","insertpagebefore","navigationpage","platform"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}