In this post we’re gonna look at Page states and the Mango Life Cycle.
Let’s create a new project and let’s select the default template for Windows Phone Project.
If you’re new to Windows Phone 7 SDK, this time you get the chance to select whether the Windows SDK will be 7.1 as Mango as suppose to 7.0.
Let’s create two pages for our demo app.
In our MainPage.xaml let’s try adding a HyperLink Button linking it to our page 1, so in our xaml.
Now on page 1 let’s create another hyperlink button where it will link us to page 2.
Now if we run our application and if we click the hyperlink button we will navigate forward to page 1.
This text box represent page state. Now as a user, we can input on the text box to fill up something.
Now in 7.0 what we happens is that even if the application gets deactivated by user pressing the Start/Search button or the phone is receiving an incoming call and returns back to the application is that the user’s input text should be saved and not load the default value which is an empty string. That is where we handle the application life cycle.
In Mango 7.5, if you try to press the search/start button and press the back button that process is now called the Fast App Resume. The reason is that by default, the debugger simulates dormancy (Dormant state) when the application gets deactivated and that is the reason the page state is restored even if you did not handle anything in your App.xaml
Note: As we know there are chances that in case the operating system runs low on resources that are dormant applications will get tombstoned in the later state.
The question now is, so how do we test what happens with our page state if that happens?
– A typical scenario is that we run a bunch of different applications and hope that the OS run out of resources but fortunately there is an easier way to do this.
Let’s try to simulate this by going to the Properties of our Project.
Right click -> Select Properties -> Debug tab
Now notice in our debug window is that there is a check box to check whether we want the application to run tombstoning upon deactivation or not.
Remember in my previous post that I mentioned that when an application is tombstoned, it automatically kills the threads and processes. So if we try to return back to our application it will create a new instance of the threads and processes and it takes MORE TIME. This scenario is what is happening in Windows Phone 7
Here is the code for us to implement saving page states in Mango it’s the same with 7.0…
In our Page1.xaml.cs
Again, remember that when the application is tombstoned it CREATES a new instance of the threads and processes, so everything is loaded by default.
In the code shown above, we created a boolean variable where it checks whether the application is a new instance or not.
Now where do we Save and Restore Page states? Remember the OnNavigatedFrom and OnNavigatedTo methods that you can override?
- OnNavigatedFrom is fired if the application has switched to another page or exits the current page. Let’s say going to Page 2.
- OnNavigatedTo is fired if the application is going back to the page, let’s say you press the start button and launches again the application.
- State[“”] dictionary is now what you use to save and restore page state.
In this post, you should now have an idea on how EASY it is to handle page states in Mango as opposed to 7.0.