{"record":{"id":"2044f4a68b28280a","repo":"PrismLibrary/Prism","slug":"navigationexception-cannotpopapplicationmainpage","errorCode":"NavigationException.CannotPopApplicationMainPage","errorMessage":"NavigationException.CannotPopApplicationMainPage","messagePattern":"NavigationException\\.CannotPopApplicationMainPage","errorType":"exception","errorClass":"NavigationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/PageNavigationService.cs","lineNumber":1349,"sourceCode":"\n        return false;\n    }\n\n    internal static bool UseReverseNavigation(Page currentPage, Type nextPageType)\n    {\n        return MvvmHelpers.HasNavigationPageParent(currentPage) && MvvmHelpers.IsSameOrSubclassOf<ContentPage>(nextPageType);\n    }\n\n    private INavigationResult SendAppToBackground(Page page)\n    {\n#if ANDROID\n        if (Window.Handler.PlatformView is MauiAppCompatActivity activity)\n        {\n            activity.MoveTaskToBack(true);\n            return new NavigationResult();\n        }\n#endif\n        throw new NavigationException(NavigationException.CannotPopApplicationMainPage, page);\n    }\n\n    private INavigationResult Notify(NavigationRequestType type, INavigationParameters parameters, Exception exception = null)\n    {\n        var result = new NavigationResult(exception);\n        _eventAggregator.GetEvent<NavigationRequestEvent>().Publish(new NavigationRequestContext\n        {\n            Parameters = parameters,\n            Result = result,\n            Type = type,\n        });\n\n        return result;\n    }\n\n    private INavigationResult Notify(Uri uri, INavigationParameters parameters, Exception exception = null)\n    {\n        var result = new NavigationResult(exception);","sourceCodeStart":1331,"sourceCodeEnd":1367,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/PageNavigationService.cs#L1331-L1367","documentation":"Prism's PageNavigationService throws this when a GoBackAsync would pop the application's main (root) page. On MAUI the root PrismWindow page cannot be removed; on Android Prism instead moves the task to the background (MoveTaskToBack), but on other platforms it refuses with CannotPopApplicationMainPage because removing the root page would leave the window empty.","triggerScenarios":"Calling navigationService.GoBackAsync() (or a URI with '../' segments that consumes the root) when the current page is the application's root/main page of the window; GoBackToRootAsync edge cases where the main page is on the stack.","commonSituations":"Hardware/software back button handling on the login/home page; a 'back' command wired to the root page after a fresh app start; navigation stack shrunk to one page (root) but UI still shows a back affordance.","solutions":["Guard the back call: only call GoBackAsync when CanNavigate (more than one page in the stack).","On Android, rely on Prism's MoveTaskToBack behavior and handle the root page's back explicitly (e.g. SystemNavigationManager or activity behavior).","Replace the root instead of popping: use NavigateAsync with the absolute URI ('app://...') or NavigateAsync(\"/NewRoot\") semantics appropriate for your flow.","Hide/disable back buttons on the root page so users cannot trigger the pop."],"exampleFix":"// before\nprivate async void OnBack() => await _navigationService.GoBackAsync();\n\n// after\nprivate async void OnBack()\n{\n    if (App.Current.Windows[0].Page.Navigation.NavigationStack.Count > 1)\n        await _navigationService.GoBackAsync();\n}","handlingStrategy":"validation","validationCode":"var navStack = App.Current.Windows[0].Page.Navigation.NavigationStack;\nbool isRoot = navStack.Count <= 1;\nif (!isRoot)\n    await _navigationService.GoBackAsync();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Hide back buttons when the page is the window's root page.","Check NavigationStack.Count before popping.","Replace root pages via absolute navigation rather than popping."],"tags":["navigation","maui","main-page","go-back"],"backgroundTag":"invalid-state-transition","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}