> ## Content Index
> Fetch the complete content index at: https://developerinsider.co/llms.txt
> Use this file to discover other available public pages before exploring further.

# Handle back button pressed in UWP
- URL: https://developerinsider.co/handle-back-button-pressed-in-uwp/
- Published: 2016-03-13T08:17:44.000Z
- Updated: 2018-03-29T17:00:49.000Z
- Author: Vineet Choudhary
- Tags: Windows

This article is all about handling back button pressed in Universal Windows Platform (PC + Mobile). We will use the [SystemNavigationManager](https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.systemnavigationmanager?ref=developerinsider.co) of `Windows.UI.Core` namespace in order to handle.

#### Handle back button for single page

---

If you just want to handle navigation for single page. Follow the following steps

**Step 1**. Use namespace `Windows.UI.Core`

```
using Windows.UI.Core;

```

**Step 2.** Register back request event for current view. Best place for this is main constructor of class after `InitializeComponent()`.

```
public MainPage()
{
    this.InitializeComponent();
    //register back request event for current view
    SystemNavigationManager.GetForCurrentView().BackRequested += MainPage_BackRequested;
}

```

**Step 3.** Handle BackRequested event

```
private void Food_BackRequested(object sender, BackRequestedEventArgs e)
{
    if (Frame.CanGoBack)
    {
        Frame.GoBack();
        e.Handled = true;
    }
}

```

#### For Complete Application at one place for single rootFrame

---

Best place for handling all backbutton for all Views is `App.xaml.cs`

**Step 1**. Use namespace `Windows.UI.Core`

```
using Windows.UI.Core;

```

**Step 2.** Register back request event for current view. Best place for this is `OnLaunched` just before `Window.Current.Activate` and create an event handle for rootFame `Navigated` event to handle visibility of `AppViewBackButton` (for PC)

```
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    rootFrame = new Frame();
    rootFrame.Navigated += OnNavigated;
    ...
    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    Window.Current.Activate();
}

```

**Step 3.** Handle BackRequested event

```
private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame.CanGoBack)
    {
        rootFrame.GoBack();
        e.Handled = true;
    }
}

```

**Step 4.** Manage visibility of `AppViewBackButton` (for PC)

```
private void OnNavigated(object sender, NavigationEventArgs e)
{
    // Each time a navigation event occurs, update the Back button's visibility
    SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
        ((Frame)sender).CanGoBack ?
        AppViewBackButtonVisibility.Visible :
        AppViewBackButtonVisibility.Collapsed;
}

```

---