thongaduka
thongaduka

Reputation: 627

How to load MainPage when goBack(); form SecondPage?

I use NavigationService.Navigate("..."). But I want to load MainPage again when I click the Back button by event goBack();

Upvotes: 0

Views: 711

Answers (8)

user3811205
user3811205

Reputation: 195

I don't recommend overriding the OnBackKeyPress method and calling NavigationService.Navigate method within it as it may mess up your page back stack.

I assume your app has more than 2 pages. Otherwise you should just call NavigateService.GoBack() as others have recommended.

Example

  1. Start on MainPage (backstack: empty)
  2. Navigate to SecondPage (backstack: MainPage)
  3. Navigate to ThirdPage (backstack: SecondPage, MainPage)
  4. Click the back button (overridden) and you are taken to MainPage (backstack: ThirdPage, SecondPage, MainPage)
  5. Click the back button and you are taken to the ThirdPage (backstack: SecondPage, MainPage). This is not the expected behaviour as the application should have exited.
  6. Now you're stuck in a back button loop.

Solution

Override OnNavigatingFrom and remove any of the backstack entries you don't want the user to go to. You can either allow the user to press the back button to get back to the MainPage from the ThirdPage or you can call NavigationService.GoBack().

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    base.OnNavigatingFrom(e);

    // Remove all backstack entries except for the first one
    while(NavigationService.BackStack.Count() > 1)
    {
        NavigationService.RemoveBackEntry();
    }
}

Upvotes: 0

user2627787
user2627787

Reputation: 1

if WP7 or WP8 : NavigationService.GoBack()

if WP8.1 : Frame.GoBack(); and you can Override the navigated to event

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {

// do nothing  }

Upvotes: 0

Shafiq Abbas
Shafiq Abbas

Reputation: 492

There are two ways to get back to your MainPage.

(First Method) Override the OnBackKeyPress() and write the following snippet inside it.

NavigationService.GoBack();

Note :

This will move to the MainPage only if the MainPage is the previous page in your Navigation history.

(Second Method) Override the OnBackKeyPress() and write the following snippet inside it.

NavigationService.Navigation(new Url("/MainPage.xaml",UriKind.Relative));

Note :

This will create a New MainPage and navigates to it. So if you navigated from MainPage to SecondPage and if you navigate again to MainPage using the above snippet, you will be having two MainPage in your Navigation history.

So, use any one of the snippet according to your scenario.

Upvotes: 0

Rashad Valliyengal
Rashad Valliyengal

Reputation: 3162

You can override the method OnBackKeyPress, and inside the method write code shown below, it ll navigate to MainPage.xaml

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}

Upvotes: 2

Skak2000
Skak2000

Reputation: 84

If you are using binding do this:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
       App.ViewModel.LoadData();

    }

And in MainViewModel.cs

public void LoadData() { Items.Clear();
this.Items.Add(new ItemViewModel() { LineOne = "runtime one", LineTwo = "Data info 2" }); this.IsDataLoaded = true; }

Upvotes: 1

Labrinths
Labrinths

Reputation: 130

Override the navigated to event

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
       if (PhoneApplicationService.Current.State["msg"].ToString() == "from page2")
       {
         // do something when its comming from page2
       }
        base.OnNavigatedTo(e);
    }

Now that will trigger every time its navigated to but you could send some parameters so you know what to depending on what page it came from.

PhoneApplicationService.Current.State["msg"] = "from page2";
NavigationService.Navigate(new Uri("/MainPage.xaml?msg=", UriKind.Relative));

Upvotes: 0

James Croft
James Croft

Reputation: 1680

Like Nigel said, you can use NavigationService.GoBack() however this will only work if you went from the MainPage to the second page.

The only other way to do this is how you have mentioned using NavigationService.Navigate(uri).

Upvotes: 0

Nigel Sampson
Nigel Sampson

Reputation: 10609

In your goBack method you can use the NavigationService.GoBack() call to move from SecondPage back to the MainPage. You can view all the Navigation methods available to you on MSDN.

Upvotes: 4

Related Questions