Search code examples
.netxamarinmauimaui-community-toolkit

.NET Maui - Navigating back from the page requires double-tapping


I am building a .NET MAUI Android and iOS application and I implemented page in which I pick employees from the picker component, I also implemented clearing of the picker selection in OnDisappearing method.

This is the code-behind:

public partial class APAchievedBenefits
{
    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        _adminAchievedBenefitsViewModel.AchievedBenefitsForEmployee.Clear();
    }
}

The issue occures when i try to navigate-back on my back button from the page. I need to do it twice when no-element is selected, but in case one element is selected then it works properly.


Solution

  • I managed to resolve this by providing an additional if statement that will check if my current page is not-present in the navigation stack.

    This will always be true since we're in the OnDissapearing method, and by doing that we're only clearing our elements from picker upon navigating-back from our page.

    Here is the code modification:

    protected override void OnDisappearing()
        {
            base.OnDisappearing();
            if (!Navigation.NavigationStack.Contains(this))
            {
                _adminAchievedBenefitsViewModel.AchievedBenefitsForEmployee.Clear();
            }
        }