You are on page 1of 23

TPA - Windows Phone

Navigation, Application lifecycle

Prepared by: Kamil Kowalski

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.

There are three main ways to navigate between pages in WP application:


Using the PhoneApplicationPage virtual methods for handling the result of navigation. HyperlinkButton - it provides NavigateUri property which can be used to navigate to a page Uri. NavigationService - it provides methods, properties, and events to support navigation within a wp7 application.

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.

Passing Data between Pages


form a query string use the NavigationContext.

Navigation controls: Panorama & Pivot Navigation helper: Navigator class (full solution code) Windows Phone Navigation - Advanced Recipes

Application life cycle


There can be only one! (..application running at a time)

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

In Windows Phone 7 you should think of the lifecycle as:


Start Run Interrupted execution (application data saved) or exit If interrupted, come back (application data restored)or, even if interrupted, start anew If exited, start anew

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

Application life cycle


events

Application start invokes the Launching event handler


The Launching event is raised when a new instance of the application is launched by the user. At this stage the user expect a new fresh instance of the application and quickly wants to start using it. Never put code in this event handler that takes time, really avoid all resource heavy tasks and perform them when the application is loaded and perform these resource intensive tasks on a background thread.

Running state - once the application is launched it is in the running state.


Application will continue to run until the user navigates away from it or if the lock screen engages. As a developer you should never provide the user with a button to exit or quit the application!

Application life cycle


events

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!

Application life cycle


events

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.

Application life cycle


events

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.

using Microsoft.Phone.Shell gives access to a State dictionary


Similar to page dictionary, but global - not page instance specific

Basic usage:
PhoneApplicationService.Current.State["mykey"] = "Value";
if PhoneApplicationService.Current.State.ContainsKey("mykey")) if(PhoneApplicationService.Current.State.TryGetValue("mykey", out str)

Application life cycle


events

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.

Application life cycle


events

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.

Application life cycle


events

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.

Application life cycle


events

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.

Application life cycle


Summary of all the states, methods and events
Launching Event - Execute very little code, no resource heavy tasks OnNavigatedTo - Check if it is a new instance. If not check if there is data in State and restore it OnNavigatedFrom - If not a backward navigation, save information to the State Dictionary Deactivated Event - Save application state to State dictionary. Also save persistent state to isolated storage that needs to be restored between application instances in case of application termination. Activated Event - Check IsApplicationInstancePreserved property. If true (application was dormanted) do nothing. If false, use State dictionary to restore application state. Closing Event - Save persistent application data to isolated storage

Application life cycle


What should I save? Panorama: Panorama.SelectedIndex Pivot: Pivot.SelectedIndex ListBox: ScrollViewer.VerticalOffset and ScrollViewer.HorizontalOffset TextBox: TextBox.Text, TextBox.SelectionStart, and TextBox.SelectionLength CheckBox: CheckBox.IsChecked RadioButton: RadioButton.IsChecked Slider: Slider.Value

Q&A
??

Application life cycle


Saving page state

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

You might also like