DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > GoBack vs. Navigate method in Windows Phone

GoBack vs. Navigate method in Windows Phone

Jevgeni Tšaikin user avatar by
Jevgeni Tšaikin
·
Dec. 28, 11 · Mobile Zone · Interview
Like (0)
Save
Tweet
8.63K Views

Join the DZone community and get the full member experience.

Join For Free

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/


Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Are All Kubernetes Ingresses the Same?
  • SQL GROUP BY and Functional Dependencies: a Very Useful Feature
  • Understanding Kubernetes Resource Types
  • 8 Must-Have Project Reports You Can Use Today

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo