Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] MAUI FlyoutPage CanNavigateAsync not called #2918

Open
berhir opened this issue Jul 31, 2023 · 1 comment
Open

[BUG] MAUI FlyoutPage CanNavigateAsync not called #2918

berhir opened this issue Jul 31, 2023 · 1 comment

Comments

@berhir
Copy link
Contributor

berhir commented Jul 31, 2023

Description

Same as #2914 but with a sample attached.

Some of our users managed to accidentally close the app by swiping back on the main page on Android.
To prevent this, I tried to implement the IConfirmNavigationAsync interface in our MainPageViewModel.

public Task<bool> CanNavigateAsync(INavigationParameters parameters)
{
    if (parameters.GetNavigationMode() == Prism.Navigation.NavigationMode.Back)
    {
        return Task.FromResult(false);
    }

    return Task.FromResult(true);
}

Unfortunately, this method never gets called. Instead, I see an exception in our logs:

Navigation failed
Prism.Navigation.NavigationException: Cannot Pop Application MainPage
   at Prism.Navigation.PageNavigationService.GoBackAsync(INavigationParameters parameters)

We are using a FlyoutPage as main page.

I created a sample here: https://github.com/berhir/Prism/tree/PrismFlyoutCanNavigateIssue/PrismFlyoutCanNavigate

I tested it on Android and the CanNavigateAsync method of the MainViewModel never gets called.
See https://github.com/berhir/Prism/blob/PrismFlyoutCanNavigateIssue/PrismFlyoutCanNavigate/MainViewModel.cs

You can test my workaround by uncommenting the code in this file: https://github.com/berhir/Prism/blob/PrismFlyoutCanNavigateIssue/PrismFlyoutCanNavigate/MainPage.xaml.cs

Steps to Reproduce

  1. Create a new MAUI project with Prism
  2. Use a FlyoutPage as main page
  3. Implement the IConfirmNavigationAsync interface and disallow navigating back on the MainPageViewModel
  4. Run the app and swipe back on the main page

Expected result: The IConfirmNavigationAsync.CanNavigateAsync method gets called and I can prevent that the app gets closed.
Actual result: The IConfirmNavigationAsync.CanNavigateAsync method gets not called and the app gets closed.

Platform with bug

.NET MAUI

Affected platforms

Android

Did you find any workaround?

As a workaround I override the OnBackButtonPressed method of the FlyoutPage:

protected override bool OnBackButtonPressed()
{
    // The FlyoutPage is the container and OnBackButtonPressed gets also called when navigating back in the details navigation page
    if (Detail is NavigationPage navPage && (navPage.Navigation.NavigationStack.Count > 1 || navPage.Navigation.ModalStack.Count > 1))
    {
        return base.OnBackButtonPressed();
    }

    return true;
}

Relevant log output

No response

@berhir
Copy link
Contributor Author

berhir commented Jan 29, 2024

Maybe related to #2990?

@dansiegel dansiegel added the MAUI label Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants