Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Correct OnBackPressed return value #107

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Prism.Maui/Common/MvvmHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ public static void SetCurrentPageDelegate(Func<Page, Page> getCurrentPageDelegat
return GetOnNavigatedToTargetFromChild(page);
};

public static async Task HandleNavigationPageGoBack(NavigationPage navigationPage)
public static async Task<INavigationResult> HandleNavigationPageGoBack(NavigationPage navigationPage)
{
var navigationService = Navigation.Xaml.Navigation.GetNavigationService(navigationPage.CurrentPage);
await navigationService.GoBackAsync();
return await navigationService.GoBackAsync();
}

public static void HandleSystemGoBack(IView previousPage, IView currentPage)
Expand Down
17 changes: 16 additions & 1 deletion src/Prism.Maui/Controls/PrismNavigationPage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Prism.Common;
using Prism.Navigation;

namespace Prism.Controls;

Expand All @@ -25,6 +26,20 @@ protected override bool OnBackButtonPressed()

private async void HandleBackButtonPressed(object sender, EventArgs args)
{
await MvvmHelpers.HandleNavigationPageGoBack(this);
bool backingOut = false;

try
{
var result = await MvvmHelpers.HandleNavigationPageGoBack(this).ConfigureAwait(false);
}
catch (NavigationException ex)
{
backingOut = ex.Message == NavigationException.CannotPopApplicationMainPage;
}
Comment on lines +31 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should never occur as the NavigationService is meant to catch any exception it encounters and wrap it in the INavigationResult. If we are aren't catching this for some reason it is a bug. Also this wouldn't be where we want to handle the logic to back out as the Navigation Stack may not contain a PrismNavigationPage as the root Page.


if (backingOut)
{
Application.Current.Quit();
}
}
}
11 changes: 3 additions & 8 deletions src/Prism.Maui/PrismAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ internal PrismAppBuilder(IContainerExtension containerExtension, MauiAppBuilder
{
var root = ContainerLocator.Container;
if (root is null)
return true;
return false;

var app = root.Resolve<IApplication>();
var windows = app.Windows.OfType<PrismWindow>();
if (!windows.Any(x => x.IsActive))
return true;
return false;

var window = windows.First(x => x.IsActive);
var currentPage = window.CurrentPage;
Expand All @@ -60,13 +60,8 @@ internal PrismAppBuilder(IContainerExtension containerExtension, MauiAppBuilder
if (dialogContainer.Dismiss.CanExecute(null))
dialogContainer.Dismiss.Execute(null);
}
else
{
var navigation = container.Resolve<INavigationService>();
navigation.GoBackAsync();
}

return false;
return true;
});
});
#endif
Expand Down