Different Way to Use Back Button Event in Windows Phone 8.1

Different ways to use Back Button press event in windows phone:

  1. Using hardware button event (It will fire from all pages of application while you write event on a single page)

    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    event
    1. void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)  
    2. {  
    3.   
    4.    Application.Current.Exit();  
    5.   
    6. }  
  2. If you want back button will work on any individual page then use following method.
    1. protected override void OnNavigatedFrom(NavigationEventArgs e)  
    2. {  
    3.     this.navigationHelper.OnNavigatedFrom(e);  
    4.   
    5.     base.OnNavigatedFrom(e);  
    6.   
    7.     if (e.NavigationMode == NavigationMode.Back)  
    8.     {  
    9.         App.Current.Exit();  
    10.     }  
    11.   
    12. }  
  3. To override button event in windows phone device.
    1. protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)  
    2.   
    3. {  
    4.   
    5.     e.Cancel = true;  
    6.   
    7.     base.OnBackKeyPress(e);  
    8.   
    9. }  

When we uses first method then it will fire (close the application) form any page. Means event will fire from all page when back button pressed. But if we want that will work on any particular page the 2nd method will be the best.