{"record":{"id":"217fc21f44daa8f8","repo":"dotnet/maui","slug":"this-should-never-happen-please-file-a-bug","errorCode":null,"errorMessage":"This should never happen, please file a bug","messagePattern":"This should never happen, please file a bug","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/AppCompat/NavigationPageRenderer.cs","lineNumber":560,"sourceCode":"\t\t\telse if (e.PropertyName == NavigationPage.IconColorProperty.PropertyName)\n\t\t\t\tUpdateToolbar();\n\t\t}\n\n\t\tvoid DeviceInfoPropertyChanged(object sender, DisplayInfoChangedEventArgs e)\n\t\t{\n\t\t\tResetToolbar();\n\t\t}\n\n\t\tvoid InsertPageBefore(Page page, Page before)\n\t\t{\n\t\t\tif (!_isAttachedToWindow)\n\t\t\t\tPushCurrentPages();\n\n\t\t\tUpdateToolbar();\n\n\t\t\tint index = PageController.InternalChildren.IndexOf(before);\n\t\t\tif (index == -1)\n\t\t\t\tthrow new InvalidOperationException(\"This should never happen, please file a bug\");\n\n\t\t\tFragment fragment = FragmentContainer.CreateInstance(page);\n\t\t\t_fragmentStack.Insert(index, fragment);\n\t\t}\n\n\t\tvoid OnInsertPageBeforeRequested(object sender, NavigationRequestedEventArgs e)\n\t\t{\n\t\t\tInsertPageBefore(e.Page, e.BeforePage);\n\t\t}\n\n\t\tvoid OnPopped(object sender, NavigationRequestedEventArgs e)\n\t\t{\n\t\t\te.Task = PopViewAsync(e.Page, e.Animated);\n\t\t}\n\n\t\tvoid OnPoppedToRoot(object sender, NavigationRequestedEventArgs e)\n\t\t{\n\t\t\te.Task = PopToRootAsync(e.Page, e.Animated);","sourceCodeStart":542,"sourceCodeEnd":578,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/AppCompat/NavigationPageRenderer.cs#L542-L578","documentation":"Thrown by NavigationPageRenderer.InsertPageBefore when IndexOf(before) on PageController.InternalChildren returns -1, meaning the 'before' page is not present in the NavigationPage's logical children. This is an assertion-style guard: the navigation request references a page that the renderer's fragment stack and the NavigationPage's child list disagree on, indicating an internal state inconsistency. The message explicitly asks for a bug report.","triggerScenarios":"Calling NavigationPage.InsertPageBefore with a 'before' page that was never pushed, already popped, or belongs to a different NavigationPage. Can also arise from a race where the page is removed concurrently, or after a fragment state restoration mismatch where InternalChildren and _fragmentStack diverge.","commonSituations":"Programmatic navigation logic that inserts before a stale page reference. Fragment restoration after process death where InternalChildren were not rebuilt. Concurrency between navigation events and collection modification. Known edge cases in older Forms versions around InsertPageBefore during animated transitions.","solutions":["Before calling InsertPageBefore, verify NavigationPage.NavigationStack contains the 'before' page and that it is not null.","Avoid issuing InsertPageBefore while a push/pop animation is in flight; defer navigation until the previous operation's task completes.","If using fragment restoration, set AllowFragmentRestore appropriately and rebuild the stack from the authoritative NavigationStack rather than relying on restored fragments."],"exampleFix":"// before\nnavPage.InsertPageBefore(newPage, beforePage); // throws if beforePage not in stack\n\n// after\nif (navPage.NavigationStack.Contains(beforePage))\n    navPage.InsertPageBefore(newPage, beforePage);\nelse\n    await navPage.PushAsync(newPage);","handlingStrategy":"validation","validationCode":"// Before InsertPageBefore, confirm 'before' is on the NavigationPage stack.\nbool CanInsertBefore(NavigationPage nav, Page before)\n{\n    return before != null && nav.NavigationStack.Contains(before);\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    navPage.InsertPageBefore(newPage, beforePage);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"never happen\"))\n{\n    // The 'before' page is not in the stack; refresh from the authoritative NavigationStack.\n    Log.Error(nameof(NavigationPage), $\"InsertPageBefore failed; '{beforePage}' not in stack.\");\n}","preventionTips":["Always check NavigationPage.NavigationStack.Contains(before) before calling InsertPageBefore.","Avoid concurrent navigation; await the previous navigation Task before issuing the next.","Disable fragment restoration (AllowFragmentRestore) if you rebuild the stack from scratch on restart.","Report reproducible cases upstream since the message requests a bug report."],"tags":["android","navigationpage","insertpagebefore","internal-state","assertion"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}