GoBack vs. Navigate method in Windows Phone
Join the DZone community and get the full member experience.
Join For FreeRecently 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>
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>
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.
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.
Opinions expressed by DZone contributors are their own.
Comments