0

I am implementing password lock feature that my application supports.

  1. My application has been executed.
  2. And user navigated some pages to pages.
  3. The user click home button so my app has been suspended.
  4. The user wants to open my app again, so my app resumes.
  5. Now my application shows(in resuming handler) the password lock page to user.
  6. If user entered correct password, user can continue exploring application with backstacked pages. Everything is perfect.
  7. But in 5 case, what if user click hardware back button?
    In this case, my application closes my passwordlock page and shows (my application's) other page in backstack. Obviously wrong behavior.
    I want to make navigating, in that case, to other application page the user had been before. It could be home screen or facebook.

How can I handle this scenario?

Benjamin
  • 10,085
  • 19
  • 80
  • 130
  • Check [where your navigation is performed](http://stackoverflow.com/a/24336005/2681948) and before navigating perform some actions, maybe clear backstack, navigate to home page, this depends on your needs. – Romasz Aug 05 '14 at 09:07

3 Answers3

0

In general, you can "override" the hardware back button's behavior by implementing the BackPressed event.

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    // ...add custom navigation code here
}

See Handling the Back Button in a Windows Phone app (XAML) for details

andreask
  • 4,248
  • 1
  • 20
  • 25
  • In WP8.1 Runtime you don't override back button, what you can do is to subscribe to an event. Depending on OP's code he will have to check some eventhandlers. – Romasz Aug 05 '14 at 09:08
0

Sure. Here you go:

// when suspending
var frame = Window.Current.Content as Frame;
var state = frame.GetNavigationState();
ApplicationData.Current.LocalSettings.Values["state"] = state;

// when resuming
var frame = (Window.Current.Content = new Frame()) as Frame;
frame.Navigate(typeof(Login));

// after login success
var frame = Window.Current.Content as Frame;
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("state"))
{
    var state = ApplicationData.Current.LocalSettings.Values["state"] = state;
    frame.SetNavigationState(state);
}
else
{
    // no state to load
    frame.Navigate(typeof(MainPage));
}

Best of luck!

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
0

You can "override" the hardware back button's behavior BackPressed event for what you want in this case.

Well this code will only work in Windows Phone, because there is no hardware back button in Windows 8 systems( since you added the Windows-runtime tag ).

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    e.Cancel = true;
}

This code will cancel back button's job and the app won't load the page in the stack, so it will stay in password page. You can change the behaviour as you want like this:

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    if( has_entered_password )
        this.Frame.Navigate(typeof(__PageYouWant__));
    else
        e.Cancel = true;
}
sertsedat
  • 3,490
  • 1
  • 25
  • 45