Scott
Scott

Reputation: 1021

UWP Tablet Mode back button not working

I have UWP app that implements the below code to wire up the system back button. My understanding is that this event is provided to capture hardware back buttons on Windows Phones, the back button in the title bar on Windows 10 and the back button on the task bar in Windows 10 tablet mode.

The hardware and title bar back buttons are working in my app, but when in tablet mode, pressing the back button on the task bar moves my app to the background and navigates to the Start Menu regardless of where I am in the app backstack. The BackRequested event IS firing in this case and my app is navigating back one page.

protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
  Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested +=
      App_BackRequested;
}

private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
   NavService.GoBack();
}

Any thoughts on why the tablet mode back button would behave this way? I'm seeing this behavior across many Windows 10 PCs, Surfaces, etc.

Upvotes: 0

Views: 559

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39082

The default behavior of the Tablet mode back button is indeed to navigate out of the app. To prevent this you have to make sure that when you can navigate back in the app, you also mark the back navigation as handled.

private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
   if ( NavService.CanGoBack() )
   {
      NavService.GoBack();       
      e.Handled = true;
   }
}

You will have to add a CanGoBack() method that will check the app Frame's CanGoBack property.

Upvotes: 2

Related Questions