title: Back button on Windows Phone 8
body: <p>I am having trouble getting the back hardware button to do what I would like it to do for the Windows Phone 8.  The app is strictly just webview, so as of now when a back (hardware) button is clicked it closes the app.  How can I work around this so it goes to the previous webpage or goes back to the index or something on those lines?</p>
  
  <p>Thanks</p>
  
  <p>This is what I currently have in the MainPage.xaml.cs file</p>
  
  <pre><code>namespace AvoidDiabetes
  {
  public partial class MainPage : PhoneApplicationPage
  {
      // Url of Home page
      private string MainUri = "/Html/index.html";
      private Stack&lt;Uri&gt; _history = new Stack&lt;Uri&gt;();
      private Uri _current = null;
  
      // Constructor
      public MainPage()
      {
          InitializeComponent();
      }
  
      private void Browser_Loaded(object sender, RoutedEventArgs e)
      {
          // Add your URL here
          Browser.Navigate(new Uri(MainUri, UriKind.Relative));
          Browser.IsScriptEnabled = true;
      }
  
      // Navigates back in the web browser's navigation stack, not the applications.
      private void BackApplicationBar_Click(object sender, EventArgs e)
      {
          Browser.GoBack();
      }
  
      // Navigates forward in the web browser's navigation stack, not the applications.
      private void ForwardApplicationBar_Click(object sender, EventArgs e)
      {
          Browser.GoForward();
      }
  
      // Navigates to the initial "home" page.
      private void HomeMenuItem_Click(object sender, EventArgs e)
      {
          Browser.Navigate(new Uri(MainUri, UriKind.Relative));
  
  
      }
  
      // Handle navigation failures.
      private void Browser_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
      {
          MessageBox.Show("Navigation to this page failed, check your internet connection");
      }
  
  
  
      protected override void OnBackKeyPress(CancelEventArgs e)
      {
          base.OnBackKeyPress(e);
  
  
          if (_history.Count == 0)
          {
              // No history, allow the back button
              // Or do whatever you need to do, like navigate the application page
              return;
          }
          // Otherwise, if this isn't the first navigation, push the current
          else
          {
              Browser.GoBack();
          }
  
  
  
  
      }
  
      private async void WebBrowser_Navigated(object sender, NavigationEventArgs e)
      {
          // If we navigated back, pop the last entry
          if (_history.Count &gt; 0 &amp;&amp; _history.Peek() == e.Uri)
          {
              _history.Pop();
          }
          // Otherwise, if this isn't the first navigation, push the current
          else if (_current != null)
          {
              _history.Push(_current);
          }
  
          // The current page is now the one we've navigated to
          _current = e.Uri;
      }
  
  }
  </code></pre>
  
  <p>}</p>