2

I have had a problem where on some instances in the emulator, when I click the back hardware button the back page loads with the constructor being called and some other time the constructor is not called.Why is this? Is this because its the emulator?

chugh97
  • 9,602
  • 25
  • 89
  • 136

1 Answers1

0

How are you performing navigation? Are you canceling the initial OnNavigatingFrom in order to perform an animation, then listening initiating navigation again after the animation completes?

protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
    if (_pendingNavigation == null)
    {
        VisualStateManager.GoToState(this, "LeavingPage", true);
        _pendingNavigation = e.Uri;
        e.Cancel = true;
    }

    base.OnNavigatingFrom(e);
}

void LeavingPage_Completed(object sender, EventArgs e)
{
    var uri = _pendingNavigation;
    NavigationService.Navigate(uri);
    _pendingNavigation = null;
}

The bug occurs when you call NavigationService.Navigate(), which then adds a new page instance to the navigation stack. To fix this bug, you need to check and make sure the initial page navigation is a "New" navigation. Something like so:

if (e.NavigationMode == NavigationMode.New && _pendingNavigation == null)
{
    VisualStateManager.GoToState(this, "LeavingPage", true);
   _pendingNavigation = e.Uri;
   e.Cancel = true;
}
terphi
  • 781
  • 7
  • 18