Download presentation
Presentation is loading. Please wait.
1
Windows Runtime App Lifecycle
WinRT Apps Building Apps for Windows Phone 8.1 Jump Start Building Windows Runtime Apps using C# and XAML Windows Runtime App Lifecycle
2
In this module… Windows Runtime App Lifecycle
Launch Suspend Resume Termination Saving and Restoring State
3
Lifecycle? phone resources are scarce (battery especially)
OS runs one foreground app at a time other apps are suspended and/or terminated OS has many mechanisms for an app to appear ‘alive’ OS has controlled mechanisms for background code
4
Lifecycle? lifecycle is very similar to Windows 8.1 app lifecycle
differences due to the nature of Phone hardware and the need to maintain compatibility with WP8.0, WP7.x apps different from Windows Phone 8.0 app lifecycle concepts will be familiar to any mobile app developer
5
launch, suspend, resume
6
User launches app Launch Running NotRunning
7
Application.OnLaunched override
sealed partial class App : Application { /// <summary> /// Invoked when the application is launched normally by the end user. /// </summary> /// <param name="e">Details about the launch request and process.</param> protected override async void OnLaunched(LaunchActivatedEventArgs e) // How did the app exit the last time it was run (if at all) ApplicationExecutionState previousState = e.PreviousExecutionState; // What kind of launch is this? ActivationKind activationKind = e.Kind; // ... NotRunning Running Suspended Terminated ClosedByUser Launch File Protocol VoiceCommand Etc.
8
User navigates away from your app
App is suspended All code stopped No timers tick No events fire Process still alive and in memory Code has a chance to respond (next slide) Leaves App
9
Application.Suspending event
This code has limited time to run. Doing the minimum amount of work here will improve the user’s experience of your app. NB: Only ask for a deferral if you are doing async work. A deferral does not give you any more time to suspend. sealed partial class App : Application { public App() this.InitializeComponent(); this.Suspending += OnSuspending; } private async void OnSuspending(object sender, SuspendingEventArgs e) // Ask for a deferral if you need to do async work var deferral = e.SuspendingOperation.GetDeferral(); // TODO: whatever async work you need to do when suspending deferral.Complete(); // Then mark the deferral complete
10
User returns to your app
Same app is resumed Same process, same memory so values of variables are intact All code runs Code has a chance to respond... Back Launch Switcher
11
Application.Resuming event
12/31/2018 Application.Resuming event sealed partial class App : Application { public App() this.InitializeComponent(); this.Suspending += OnSuspending; this.Resuming += OnResuming; } private void OnResuming(object sender, object e) // TODO: whatever you need to do to resume your app © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
12
The debugger is/isn’t your friend
By design - your code will not suspend/resume under the debugger The debugger has a specific feature to simulate suspend/resume
13
What to do on resuming? Check external data or conditions that might change while the app was suspended Possibly factoring in the time elapsed between suspension and resumption Examples (taken from refresh data from an online source refresh the app’s view of connectivity – online or offline? refresh sensor data such as compass, geolocation retry a networking call that may have failed while suspended refresh layout – device may have reorientated since suspension check for new data populated by background tasks or system roaming
14
Apps can be closed by the user
ClosedByUser Running NB: Back button does not close the app
15
Demo
16
Termination
17
The OS can terminate your app
System has a priority system for determining which app to terminate. User Runs Another App OS Short of Memory OS Terminates An App Terminated
18
The OS can terminate your app
OS does not wake an app to terminate it – no notification App is removed from task switcher list
19
User ‘returns’ to a terminated app
User will be unaware that the app was terminated Would often expect that their experience simply continues The developers’ job is to maintain the illusion that the app has always been running, even though it hasn’t
20
User ‘returns’ to a terminated app
User must not lose data & should continue recent activity seamlessly App may need to restore transient session state Includes page navigation history, page position/parameters & contents of input fields Every app defines ‘recent’ differently
21
Transient data versus persistent data
12:38 Work Tel: Home Tel: CONTACTS Jim Jones 12:38 CONTACTS Add Phone Mobile Phone Phone type Phone number App Data Local Folder (or roaming folder for Data) 12:38 CONTACTS all Data 12:38 Jim Jones Tel: Bob Brown Tel: Arthur Abbott Tel: State Navigate(Page2, ID param) Navigate(Page3, ID param)
22
Demo
23
Help from the framework & templates
The Frame class has [Get/Set]NavigationState method to (re)store navigation history as a String The SuspensionManager class helps with (re)storing global & Frame state in a file The NavigationHelper class wires the Page to the SuspensionManager via easy page-level events the last 2 classes are not in the blank template, you need to add “basic page” items or use another template to get them
24
For a clean, new launch of the app...
12/31/2018 For a clean, new launch of the app... OnLaunched App Frame Suspension Manager RegisterFrame() allows suspension manager to; get/set navigation history be aware as the Frame is navigating © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
25
Only if the app was previously terminated
12/31/2018 Only if the app was previously terminated OnLaunched App Suspension Manager RestoreAsync() © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
26
this history is tracked by the Frame. parameters must be serializable.
12/31/2018 as the app navigates state dictionaries owned by SuspensionManager. entries must be serializable Page A Navigation Helper LoadState( Parameter, null /* dictionary */ ); Navigate(PageA, Parameter) this history is tracked by the Frame. parameters must be serializable. © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
27
12/31/2018 save all state necessary to reconstruct as page may not be cached and/or app may be terminated before page is revisited. as the app navigates Page A Page B Navigation Helper LoadState( Parameter, null /*dictionary*/ ); Navigate(PageB, Parameter) SaveState( dictionary ); Navigation Helper © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
28
12/31/2018 page may be a new or a cached instance. depends on cache mode & memory. as the app navigates Page A Navigation Helper Page B GoBack() LoadState( parameter, dictionary ); SaveState( dictionary ); Navigation Helper © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
29
as the app navigates null dictionary? - reset controls to defaults.
12/31/2018 as the app navigates null dictionary? - reset controls to defaults. non null dictionary? – restore control state. Page A Page B Navigation Helper LoadState( Parameter, dictionary ); GoForward() SaveState( dictionary ); Navigation Helper © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
30
as the app suspends ... Suspending App Suspension Manager SaveAsync()
12/31/2018 as the app suspends Suspending App Suspension Manager SaveAsync() State Dictionaries Global Frame 1 Page 1 ... © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
31
‘Recent’ state with SuspensionManager
On startup, you might want a previously terminated app to discard state and start afresh (example: state is not “recent” enough) Can modify SuspensionManager to add this capability
32
Demo
33
Related topics Notification mechanisms
12/31/2018 Related topics Notification mechanisms Session 10: Tiles, badges, Toasts and Action Center Additional activation scenarios – e.g. protocol activation Session 14: Sharing Files and Data Background working Session 11: Background Tasks Application Lifecycle in Silverlight Apps Building Apps for Windows Phone 8 Jump Start - Session 5: Windows Phone 8 Application Lifecycle © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.