DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

GoBack vs. Navigate method in Windows Phone

Jevgeni Tšaikin user avatar by
Jevgeni Tšaikin
·
Dec. 28, 11 · Interview
Like (0)
Save
Tweet
Share
8.78K 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

  • Cucumber.js Tutorial With Examples For Selenium JavaScript
  • Distributed Tracing: A Full Guide
  • Multi-Tenant Architecture for a SaaS Application on AWS
  • How To Create a Failover Client Using the Hazelcast Viridian Serverless

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: