0

I am creating somany pages in windows phone app.How to go previous page when back button pressed.when I am clcik back button it will go to first page.

for example I am in 4th page. whenever I cilck backbutton I want to go to 3rd page but it go to 1st page. Iam using below code.

public selectbus()
    {
        this.InitializeComponent();


        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

    }

    void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame != null && rootFrame.CanGoBack)
        {
             rootFrame.GoBack();
            e.Handled = true;


        }
    }

please anyone help me.

  • Where are you subscribing to this event? It's app wide and should be used once - check if you are not subscribing to it at every page. – Romasz Jul 23 '15 at 07:34
  • @romasz please see my edited code. – Bhargavi Reddy Jul 23 '15 at 07:40
  • @Romasz I am writing the above code all my pages.In constructor I am writing the code. – Bhargavi Reddy Jul 23 '15 at 08:02
  • 1
    As in the previous comment - there is a chance that the event is fired multiple times. Remove it from page constructor and try to add in app.xaml.cs. Or it may be better if you try to use BasicPage template for your pages along with SuspensionManager and more. – Romasz Jul 23 '15 at 08:03

2 Answers2

1

First, check if you navigate as the following (from page1 to page2, for example) :

this.Frame.Navigate(typeof(NameOfYourPage));

Then, on the page you want to add the HardwareButtons_BackPressed, check you have the following lines :

using Windows.UI.Xaml;
using Windows.Phone.UI.Input;

then, your code should look like the following :

//In constructor
HardwareButtons.BackPressed += HardwareButtons_BackPressed;

//Later in code
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;
    }
}

This works for me, it should work for you as well !

faflo10
  • 386
  • 5
  • 13
  • @fafio it's navigate from page2 to page1 working fine.actually what I found is in above code frame.Goback() method is there na. I am writing the above code in all my pages.whenever I click backbutton It's directly go page4 to page 1. – Bhargavi Reddy Jul 23 '15 at 07:56
  • @Bhargavi Reddy The question I have is : how do you navigate to page 4? Do you want to go like 1 --> 2 --> 3 --> 4 --> 3 or 1 --> 4 --> 3 (in your example)? The first chunk of code was to show you how I navigate (forward) from a page to another, then the others where there to show you how I navigate back. My explanation was if you wanted to go like 1 --> 2 --> 3 --> 4 then 3 <-- 4 . – faflo10 Jul 23 '15 at 08:03
  • 10 I want to go to like 4-->3-->2-->1. – Bhargavi Reddy Jul 23 '15 at 08:09
  • I want to go to only 3rd page .but it go to directly first page. – Bhargavi Reddy Jul 23 '15 at 08:10
  • @Bhargavi Reddy So, your starting page is page4? If it's so, just use `Frame.Navigate()`. If I misunderstood once again, please explain how you go from your starting page to page4 with a more precise example. – faflo10 Jul 23 '15 at 08:14
  • I want to go 4th paae to 3rd page.but it go to directly first page – Bhargavi Reddy Jul 23 '15 at 08:16
  • I am navigating to first page to second page using frame.Navigate() method.and 2nd page to 3rd page and 3rd page to 4th page like this – Bhargavi Reddy Jul 23 '15 at 08:19
  • 10 but Now I want to go back pages by clicking the back button only – Bhargavi Reddy Jul 23 '15 at 08:21
  • actually what happend is I am navigating from 1st page to 2nd page with some parameters.and also 2nd to 3rd and 3rd to 4th pages also – Bhargavi Reddy Jul 23 '15 at 08:22
  • this is my code Frame.Navigate(typeof(selectbus), Busresult1); – Bhargavi Reddy Jul 23 '15 at 08:23
  • on selectbus page protected override void OnNavigatedTo(NavigationEventArgs e) { string result = e.Parameter.ToString(); getbuses(result); } – Bhargavi Reddy Jul 23 '15 at 08:24
  • here what happend is I am go back to selectbus page this time it go to OnNavigated method.but this time I am not sending any parameters.so it shows erro on OnNavigated method. – Bhargavi Reddy Jul 23 '15 at 08:27
  • Then I agree with the comments under your question : the fact is, if your declared the Eventhandler and the method each time, It calls the method for each pages in the `frame.BackStack`, then you automatically `GoBack()` a number of times equals to your `frame.Backstack`... Meaning your first page. I suggest you to try the solution of @Romasz . – faflo10 Jul 23 '15 at 08:32
  • Thank you your response @faflo 10 – Bhargavi Reddy Jul 23 '15 at 08:50
0

Subscribe to the BackPressed event in App.xaml.cs and nowhere else. Take a look at the accepted answer here Windows Phone 8.1 Universal App terminates on navigating back from second page?.

Igor Kulman
  • 16,211
  • 10
  • 57
  • 118