3

I try to use back button in Windows Phone 8.1 application. When I start the App I'm on page A. Then I go to page B - it works well, but when I go by:

A -> B -> C and click hardware back button app goes to A

also when A -> B -> C -> D and back - also go to A

I used this code to navigate:

Frame.Navigate(typeof(StartingView));

and use this code to implement hardware back button:

HardwareButtons.BackPressed += HardwareButtons_BackPressed;

Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null && rootFrame.CanGoBack)
{
    rootFrame.GoBack();
    e.Handled = true;
}

on page D I list the Frame.BackStack and it looks well. Also when I create button with:

Frame.GoBack();

it goes from D -> C, but when i use the hardware one it goes to A.

Any idea?

Romasz
  • 29,662
  • 13
  • 79
  • 154
Criss
  • 725
  • 1
  • 6
  • 12
  • If you just want to go back on the pressed button (normal behavior) you shouldn't have to add any handlers. It would just do what you expect. I want to add a handler if you want to tell the user it's about to exit the app or your app does not allow going back, unless some action is taken. – sebagomez Nov 26 '14 at 20:18
  • Where do you subscribe to `HardwareButtons_BackPressed`? Remember that this event is app-wide so subscribing in every page without unsubscription may be a bad idea. Also set `e.Handeled = true` before `rootFrame.GoBack()`. Check if you use beside your code *NavigationHelper* or there was a subscription already in `App()` constructor. You may also take a look at [this question](http://stackoverflow.com/q/24335925/2681948). I've also added suitable tag to your question and removed tags from title (in most cases they shouldn't be there). And welcome to SO. – Romasz Nov 26 '14 at 20:39
  • I subscribe HardwareButtons_BackPressed on my start page, then I wrote only a method. I set e.Handled = true; before GoBack. Still doesnt work. – Criss Nov 26 '14 at 21:13

3 Answers3

4

I resolved it by add to App.xaml.cs in Constructor:

HardwareButtons.BackPressed += HardwareButtons_BackPressed;

and then

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {
        Frame frame = Window.Current.Content as Frame;
        if (frame == null)
        {
            return;
        }

        if (frame.CanGoBack)
        {
            frame.GoBack();
            e.Handled = true;
        }
    }

And important: removed all this handler from other views.

Criss
  • 725
  • 1
  • 6
  • 12
0

Did you check the backstack entry?

Check if the pages are there in the backstack.

string myPage = "";

foreach (PageStackEntry page in Frame.BackStack)
{
    myPage = page.SourcePageType.ToString();
}

Now see the string "mypage" on every page and make sure after navigating from each page you r getting the page name you are navigating from.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Arnab
  • 1
  • 1
0

I noticed something that could help. If you are using 'Basic Pages' as opposed to 'Blank pages' then you can remove the "hardware back pressed" things from your App.xaml.cs. This should fix the problem.

Efe Ariaroo
  • 1,052
  • 10
  • 10