wp7 tpa 5 app life cycle

Upload: damian-chmielewski

Post on 05-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    1/23

    TPA - Windows Phone

    Navigation, Application lifecycle

    Prepared by: Kamil Kowalski

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    2/23

    Agenda

    Navigation in depth Application lifecycle

    There can be only one! Events Tombstone & Dormant state

    Q & A

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    3/23

    Navigation in depth

    Windows Phone applications are based on a Silverlight pagemodel where user can navigate forward through differentscreens 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 theWindows Phone navigation model

    provide default transitions that match the Windows Phone look andfeel

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    4/23

    Navigation in depth

    Windows phone 7 navigation is based on aframe/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 screenorientation

    Exposes a client area where pages are rendered Reserves space for the system tray and application bar

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    5/23

    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 WPapplication:

    Using the PhoneApplicationPage virtual methods for handling the resultof navigation.

    HyperlinkButton - it provides NavigateUri property which can be used tonavigate to a page Uri.

    NavigationService - it provides methods, properties, and events tosupport navigation within a wp7 application.

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    6/23

    Navigation in depth

    NavigationService - contains a number of methods, eventsand 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 resourceidentifier (URI).

    For Windows Phone GoForward() will always throw an exceptionbecause 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.

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    7/23

    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

    http://msdn.microsoft.com/en-us/magazine/gg983484.aspxhttp://msdn.microsoft.com/en-us/magazine/gg983484.aspxhttp://msdn.microsoft.com/en-us/magazine/gg983484.aspxhttp://msdn.microsoft.com/en-us/magazine/gg983484.aspxhttp://msdn.microsoft.com/en-us/magazine/gg983484.aspx
  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    8/23

    Application life cycleThere can be only one! (..application running at a time)

    Windows phones have only one application running at atime = foreground application

    Switching to another application (including the startupscreen) will essentially kill the current application

    Multitasking introduced in Mango does not changesingle 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 theycan be killed, but they automatically restore their state if applicable

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    9/23

    Application lifecycle

    For desktop (WinForms / WPF) application, thier life cyclemay 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

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    10/23

    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, theunderlying 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-centricOS.

    To accommodate sessions in a process-centric world there arelogical states for the session: Launched, Running, Activated,Deactivated, Dormand , Tombstoned and Closed (or Ended).

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    11/23

    Applicationlifecycle

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    12/23

    Application life cycleevents

    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 resourceintensive 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 lockscreen engages.

    As a developer you should never provide the user with a button to exit or quit theapplication!

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    13/23

    Application life cycleevents

    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!

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    14/23

    Application life cycleevents

    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.

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    15/23

    Application life cycleevents

    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 inState 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 acrossapplication 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)

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    16/23

    Application life cycleevents

    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 tombstoningapplications.

    It will start with the application that is oldest. A developer should never assume thatthe application stays in a dormant state, if application data needs to be restored itshould be saved in isolated storage at Deactivated event.

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    17/23

    Application life cycleevents

    Tombstoned state At this state application has been terminated although information about its

    navigate state and history, and state dictionaries populated duringDeactivated are preserved .

    Max 5 tombstoned applications at time. If more applications are tombstoned the application will simply terminate .

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    18/23

    Application life cycleevents

    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 thedeveloper needs to use the State dictionary to restore application state.

    Since this event is thrown when the application starts the developer shouldnot put resource heavy operations here that halts the user experience.

    Instead do all resource intensive tasks once the application is loaded and on a backgroundthread.

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    19/23

    Application life cycleevents

    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.

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    20/23

    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

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    21/23

    Application life cycle

    What should I save? Panorama : Panorama.SelectedIndex Pivot : Pivot.SelectedIndex

    ListBox : ScrollViewer.VerticalOffset andScrollViewer.HorizontalOffset TextBox : TextBox.Text, TextBox.SelectionStart, and

    TextBox.SelectionLength

    CheckBox : CheckBox.IsChecked RadioButton : RadioButton.IsChecked Slider : Slider.Value

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    22/23

    Q & A

    ??

  • 8/2/2019 WP7 Tpa 5 App Life Cycle

    23/23