Over a million developers have joined DZone.

GoBack vs. Navigate method in Windows Phone

· Mobile Zone

Recently I have noticed that not many Windows Phone 7 developers know the difference between NavigationService.GoBack() method and NavigationService.Navigate(..Uri..) method (in case it leads to the previous page). I will explain the main difference in this tutorial.

Probably one important difference is that in case of NavigationService.GoBack method call a new instance of a Page is not created, but in case of NavigationService.Navigate(..URI of previous page..) a new instance is created and removing previously saved visual state of page. Also keep in mind that NavigationService.GoBack() method is called when user pushes the hardware back button on device. Probably everything will be much clearer after this tutorial.

Creating Pages

For the tutorial we will need 2 pages. First page will simply contain a textbox element and a button to navigate to the second page. Second page will contain two buttons: one to call NavigationService.GoBack method and another one to call NavigationService.Navigate(..URI of previous page..) method. Bellow is a XAML code and C# codebehind code I’ve used for ContentPanel element for both pages.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
 
    <Button Content="GO TO PAGE 2" Height="72" HorizontalAlignment="Left"
Margin="10,88,0,0" Name="btnGoToPage2"
VerticalAlignment="Top" Width="259" Click="button1_Click" />
 
    <TextBox Height="72" HorizontalAlignment="Left"
Margin="10,10,0,0" Text="" VerticalAlignment="Top" Width="259" />
 
</Grid>

eugenedotnet go back and navigate methods for page1 for windows phone 7 navigation

public partial class Page1 : PhoneApplicationPage
{
    public Page1()
    {
        InitializeComponent();
    }
 
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(
            new Uri("/GoBackVsNavigate/Page2.xaml", UriKind.Relative));
    }
}

Page 2

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
 
    <Button Content="GoBack" Height="72"
HorizontalAlignment="Left" Margin="10,10,0,0" Name="btnGoBack"
VerticalAlignment="Top" Width="298" Click="btnGoBack_Click" />
 
    <Button Content="Navigate To Page 1" Height="72" HorizontalAlignment="Left"
 Margin="10,90,0,0" Name="btnNavigate"
VerticalAlignment="Top" Width="298" Click="btnNavigate_Click" />
 
</Grid>

eugenedotnet go back and navigate methods for page2 for windows phone 7 navigation

public partial class Page2 : PhoneApplicationPage
{
    public Page2()
    {
        InitializeComponent();
    }
 
    private void btnGoBack_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.GoBack();
    }
 
    private void btnNavigate_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(
            new Uri("/GoBackVsNavigate/Page1.xaml", UriKind.Relative));
    }
}

 

GoBack vs Navigate

Now let’s check the difference between two types of navigation. First I will open the First Page and enter some text into textbox and push the navigate button. After page 2 has open I will press “GoBack” button and system will return to page 1 having the same text restored. To illustrate this process I have created the following image bellow.

eugenedotnet go back navigation method for windows phone 7

Now we will repeat the same process, but push “Navigate To Page 1″ button instead of “Go Back”. A new instance of Page 1 will be created making our text disappear. Same behavior applies to “tombstoning” events.

eugenedotnet navigate method for windows phone 7


Source:  http://www.eugenedotnet.com/2011/02/w18-difference-between-goback-method-and-navigate-method-of-navigationservice/


Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}