Ali Malek
Ali Malek

Reputation: 722

UWA - back button handling

I've used backrequest event like below code and on appbar title back button or mobile device hardware button I should click twice on button till event work.

What should I do to it work with just one click?

SystemNavigationManager.GetForCurrentView().BackRequested += (sender, e) =>
        {
            bool handeled = e.Handled;

            if (Frame.CanGoBack && !handeled)
            {
                handeled = true;
                Frame.GoBack();
            }

            e.Handled = handeled;
        };


        if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
        {
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += (sender, e) =>
            {
                bool handeled = e.Handled;

                if (Frame.CanGoBack && !handeled)
                {
                    handeled = true;
                    Frame.GoBack();
                }

                e.Handled = handeled;
            };
        }

Upvotes: 0

Views: 228

Answers (1)

Matt126
Matt126

Reputation: 997

I had the same issue. Every time I wanted to navigate back, I had to press the back button twice. Until I found out, that the SplitView was responsible for this problem. I had to close it, before navigating.

For example, if you want to move from Page1 to another page and then go back, use something like this on Page1:

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    MySplitView.IsPaneOpen = false;
}

I hope it helps, even if you said that on a blank project (obvious without a Hamburger menu) the result was the same.

Upvotes: 1

Related Questions