Monday, January 7, 2013

Detect (And Cancel) Windows Phone Back Key

To detect when the user presses the back key, you just have to override the OnBackKeyPress function.

Just like this:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    // to cancel the event
    e.Cancel = true;
}
You can optionally cancel the navigation or allow it to occur.

You cancel the navigation by calling the instruction on line four.

Obviously you can code any logic to conditionally cancel the navigation.

Another use is to catch the event just to persist any information and then letting the navigation occur.

Detect (And Cancel) Android Back Key

To detect when the user presses the back key, you just have to override the onBackPressed function.

Just like this:
@Override
public void onBackPressed() {
    super.onBackPressed();
}
You can optionally cancel the navigation or allow it to occur.

You cancel the navigation by not calling the instruction on like two.

Obviously you can code any logic to conditionally cancel the navigation.

Another use is to catch the event just to persist any information and then letting the navigation occur.