Professional Documents
Culture Documents
Agenda
Navigation in depth Application lifecycle
There can be only one! Events Tombstone & Dormant state
Q&A
Navigation in depth
Windows Phone applications are based on a Silverlight page model where user can navigate forward through different screens of content. Move backward is realized by Windows Phone "back" hardware button. Above model (forward / back) enables developers to:
easily create view-based applications that fit naturally into the Windows Phone navigation model provide default transitions that match the Windows Phone look and feel
Navigation in depth
Windows phone 7 navigation is based on a frame/page mode. PhoneApplicationFrame
Only a single frame is available to the application with no exceptions. It contains the Page control and another elements like system tray and application bar. Frame includes the following characteristics:
Exposes properties from a hosted page such as screen orientation Exposes a client area where pages are rendered Reserves space for the system tray and application bar
Navigation in depth
PhoneApplicationPage
A page fills the entire content region of the frame. It includes the following characteristics:
Optionally surfaces its own application bar
It supports OnNavigatedTo, OnNavigatedFrom and OnNavigatingFrom virtual methods for handling the result of navigation.
Navigation in depth
NavigationService - contains a number of methods, events and properties to help you make your navigation
Key Methods:
GoBack() - Navigates to the most recent entry in the back navigation history, or throws an exception if no entry exists in back navigation. Navigate(Uri) - Navigates to the content specified by the uniform resource identifier (URI).
For Windows Phone GoForward() will always throw an exception because there is no forward navigation stack. Key Events:
FragmentNavigation - Occurs when navigation to a content fragment begins. Navigated - Occurs when the content that is being navigated to has been found and is available. Navigating - Occurs when a new navigation is requested.
Navigation in depth
Key Properties:
CanGoBack - Gets a value that indicates whether there is at least one entry in the back navigation history. CanGoForward - Gets a value that indicates whether there is at least one entry in the forward navigation history. CurrentSource - Gets the uniform resource identifier (URI) of the content that is currently displayed. Source - Gets or sets the uniform resource identifier (URI) of the current content or the content that is being navigated to.
Navigation controls: Panorama & Pivot Navigation helper: Navigator class (full solution code) Windows Phone Navigation - Advanced Recipes
Windows phones have only one application running at a time = foreground application Switching to another application (including the startup screen) will essentially kill the current application Multitasking introduced in Mango does not change single app paradigm
Mango introduces Fast application switching and background agents
The end user should not have to be wary of applications being killed: the apps must be implemented so that they can be killed, but they automatically restore their state if applicable
Application lifecycle
For desktop (WinForms / WPF) application, thier life cycle may looks something like:
Start Run Exit Go back to 1 and start again
Application lifecycle
The benefits of this new session-oriented model are:
user can navigate across apps without having to think about how the OS is managing its resources user doesnt care if interrupting its game to reply to an incoming SMS message will kill the process for the game
The user should expect that he can get back to the game when finished with the message. If that works well enough, the underlying details are irrelevant. The downside (for developers ):
theres a bit more to handle to truly provide the perception of session continuity - your sessions are still running on a traditional, process-centric OS.
To accommodate sessions in a process-centric world there are logical states for the session: Launched, Running, Activated, Deactivated, Dormand, Tombstoned and Closed (or Ended).
Application lifecycle
OnNavigatedTo method
This method is called when the application is first launched! This method also called when the user navigates between pages in application! This method called as well when the application is relaunched after being dormant or tombstoned!
OnNavigatedFrom method
This method is called whenever a user navigates away from a page! This method also called if user navigates forward, launching another application, or launching a chooser! The application should store the page state so that it can be restored if the user navigates back to it. When navigating backward there is no need to save page state since the whole page will be re-created the next time it is visited.
Deactivated event
When the user navigates away from your application the Deactivated event is raised. In this event handler the developer needs to store transient application state in the State dictionary so that once reactivated an application can use the values stored in State dictionary to transient application state. Note that once an application is deactived it could also be terminated and its State dictionary is not preserved.
As a developer you should also store any state information/data that should be persisted across application instances to isolated storage during the Deactivated event.
Basic usage:
PhoneApplicationService.Current.State["mykey"] = "Value";
if PhoneApplicationService.Current.State.ContainsKey("mykey")) if(PhoneApplicationService.Current.State.TryGetValue("mykey", out str)
Dormant state
New state that is introduced in Mango to provide fast app switching. In this state all of the application threads are stopped and the application remains intact in memory. Since the application is kept in memory and preserved, no actions are needed when the application is reactivated. Note, that if new applications are started and these applications need more memory than is available, the operating system will start tombstoning applications.
It will start with the application that is oldest. A developer should never assume that the application stays in a dormant state, if application data needs to be restored it should be saved in isolated storage at Deactivated event.
Tombstoned state
At this state application has been terminated although information about its
navigate state and history, and state dictionaries populated during Deactivated are preserved. Max 5 tombstoned applications at time. If more applications are tombstoned the application will simply terminate.
Activated event
When the application returns from a dormant or tombstoned state the Activated event is called. The IsApplicationInstancePreserved property should be checked to determine if the application is returning from dormant or tombstoned state. Since the state is preserved in a dormant state by the operating system, nothing has to be done, if activated from a tombstoned state then the developer needs to use the State dictionary to restore application state. Since this event is thrown when the application starts the developer should not put resource heavy operations here that halts the user experience.
Instead do all resource intensive tasks once the application is loaded and on a background thread.
Closing event
When the user navigates backwards past the first page of an application it will exit and the Closing event is raised. At this stage you should not store any data in State dictionary since that will not be loaded once the application is restarted. Instead store any data that should be persisted across application instances in isolated storage. Note: The operating system has a limit of 10 seconds to complete all application and page navigation events when closing. If that time limit is exceeded, then the application will simply be terminated.
Q&A
??
Each page can use the page State dictionary to store data to local transient storage
Key-value pair after all it is a dictionary..
Data gone when returning from page, but allows proper tombstoning behavior
Forward navigation causes a new instance of the page to be created