In Windows Phone 7, is there a way to know if the back is pressed in the page that's navigated to? I know we can intercept in the current page but I need to know in the page I am navigating to. i.e. if there 2 pages say page1 and page2, back button is pressed in page2. I need to know if back button is pressed or not in page1. I need to run some stuff on back button press in page1.
6 Answers
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
...
}

- 52,123
- 16
- 112
- 141

- 251
- 3
- 2
-
This is actually better than the accepted answer. Was this available in Windows Phone 7.0 or was it introduced in 7.1/Mango? – Austin Thompson Feb 15 '12 at 18:14
-
Also you might want to put your answer in a code block so it's easier to read. I would but I can't edit it yet. – Austin Thompson Feb 15 '12 at 18:23
-
2This NavigationMode will also be true when NavigationService.GoBack() is called. If you want only the hardware press use the accepted answer. – ShawnFeatherly Aug 01 '12 at 21:06
This is kind of a hack but you can do the following; override OnBackkeyPress
event on every page. Within the event handler add the following code:
PhoneApplicationService.Current.State["isbacknav"] = true;
Then, in the OnNavigatedTo
event handler for every page, check whether the State
dictionary contains that entry.
bool isbacknav = false;
if( PhoneApplicationService.Current.State.ContainsKey( "isbacknav" ) ) {
isbacknav = (bool)PhoneApplicationService.Current.State["isbacknav"];
PhoneApplicationService.Current.State["isbacknav"] = false;
// or
// PhoneApplicationService.Current.State.Remove( "isbacknav" );
}

- 106,671
- 19
- 240
- 328
On each page you can override the OnBackKeyPress
event to detect the back key being pressed.
In that second page you could set a global flag or send a message to the first page.
There's no way for a page to know what the last page that was opened/displayed was. You'll have you to this yourself.

- 65,560
- 11
- 91
- 143
Sadly I don't think there's a built in way to do this.
When you get told about OnNavigatingFrom then the event args includes NavigationMode - http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigatingcanceleventargs.navigationmode(v=vs.95)
However, when you get told about OnNavigatedTo then the event args don't give you that information.
I think the easiest workaround might be to add a handler for NavigationService.Navigating
in your App class - you can cache the NavigationMode there. However, test this thoroughly as there may be issues about what code gets in what order - plus there may be problems with whether this code is correctly called when the user navigates back to your page from the Phone home page or from another app
There's a good explanation of NavigationService.Navigating
in http://wildermuth.com/2010/10/11/Architecting_WP7_-_Part_1_of_10_Navigation_Framework

- 66,722
- 7
- 114
- 165
I had exactly the same problem. You can see the answers here
I didn't want to reload data if I was navigating back to a page where I had already loaded data. In the end I had to use flags to determine if I had already loaded data.

- 1
- 1

- 6,873
- 3
- 54
- 82
you can always define a bool variable/s in the App class( App.xaml.cs ). they can be accessed from any page by the following method
in App class
public bool backVariable=false;
in page1 Page1.xaml.cs globally or in any method
App thisApp=Application.Current as App;
then u can access the boolean variable using
thisApp.backVariable

- 359
- 2
- 9