Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 know when back key is pressed in the "navigated to" page

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.

like image 533
Jonna Avatar asked Nov 28 '22 09:11

Jonna


2 Answers

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
    ...
}
like image 187
Kurt Webster Avatar answered Dec 11 '22 00:12

Kurt Webster


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" );
}
like image 31
Praetorian Avatar answered Dec 11 '22 01:12

Praetorian