07 wp7 application lifecycle
TRANSCRIPT
ApplicationLifecycle
Jason LeeEthos Technologies
Agenda• Interruptions & User Expectations• Application Lifecycle• Tombstoning• Lifecycle Events• Resource Management• Play Time• Q & A
Interruptions• Interruption comes from
• A user action (i.e. press the start button)• An external event (an incoming phone call)• An internal action (i.e. taking a photo, choosing a contact)
• Interruption characteristics• Interruptions are not predictable• Interruptions tend to happen frequently
User Expectations• Phone and Applications
• Phone is resource-constrained device, therefore we need to consume resources carefully
• Phone is different from Desktop OS, which can hold multiple windows running processes simultaneously
• However…• A user expects the application to be able to remain in the state where it
was left• Microsoft development guideline requires application to provide such
illusions that it keeps running continuously until it is get killed
Running
Application Lifecycle
Dormant
Phone resources detachedThreads & Timers suspendedBut app instance remains in memory
Tombstoned
Dead, and no longer lives in memory.But with somethingcarved on a Tombstone
Deactivated
Activatede.Preserved == true
Fast App Resume
Activatede.Preserved == false
You Will See Resuming Process Bar
Deactivated
Tombstoning• When an app is tombstoned, it means
• The app is dead• Some relevant data is saved by System for the app/page, which is called
State information
• When user navigates back• System restarts the application, passes applications state back to the App• System navigates to the very page where it was left, passes page state
back to the Page
• Developers should• Write code to save state before the app gets tombstoned• Write code to restore state after the app is reactivated from tombstoned
Lifecycle Events - App• Application_Launching
• Fired when the application is started• Not fired when the application is reactivated• Load app settings/configuration
• Application_Activated• Fired when the application is reactivated (brought to foreground)• Not fired when the application is started• Restore app state if tombstoned
• Application_Deactivated• Fired when the application is deactivated (sent to background)• Not fired when the application is closed• Save app state as needed
• Application_Closing• Fired when the application is closing (i.e. user presses Back key)• Not fired when the application is deactivated• Save app settings/configuration
Lifecycle Events - Page• OnNavigatedTo
• Fired when the application is started• Not fired when the application is reactivated• Restore page state if tombstoned
• OnNavigatedFrom• Fired when the application is deactivated (sent to background)• Not fired when the application is deactivated• Save page state as needed
Code Sample - 1• Application_Launching
• Load app settings/configuration
Code Sample - 2• Application_Closing
• You have only 10 seconds to finish all job• If you’ve got complex data to save, it’s recommended to save your data
while the app is running at appropriate points
Code Sample - 3• Application_Deactivated
• Save app state as needed
Code Sample - 4• Application_Activated
• Restore app state if tombstoned
Time Limits
Timer Limit Details
Startup 10 Seconds From Application start until Page's OnNavigatedTo event completes
Exit 10 Seconds From user hits Back key to Closing event completes
Return 10 SecondsFrom user invokes a return (either hitting Back key or returning from a Chooser) to Page's OnNavigatedTo event completes
Leave 10 SecondsFrom user leaves an application (i.e. hitting Start key, Search key, Locking screen) to Deactivated event completes
Resource Management - 1
XNA Audio Paused
Sensors Notifications suppressed
Networking Cancelled
Sockets Disconnected
MediaElement Disconnected
Camera Disposed
- You may release phone resources, but not necessarily, the framework is doing it anyway- You MUST Save app/page state
Deactivating App
Page.OnNavigatedFrom
App.Deactivated
Framework:Detach Resources
Framework:Suspend Timers and Threads
Dormant App
Resource Management - 2
Dormant App
XNA Audio Resumed
Sensors Notifications resumed
Networking Completed with Cancellation
Sockets -
MediaElement -
Camera -
- MediaElement : Reset Source/Position- Socket : Reconnect- Camera: Recreate Object- Restore app state if tombstoned
Framework:Resume Timers and Threads
Framework:Attach Resources
App.Activated
Page.OnNavigatedTo
Running App
Take It Serious• It is required, not just recommended !
• Microsoft tries its best to keep apps in the line, providing consistent user experience to end users.
• It is about money !• If your app does not handle lifecycle issues, then it will be disqualified to
be published on marketplace.
Play Time !• Goals
• To understand various lifecycle events and orders of occurring under different situations
• To be able to store and retrieve state information• To understand how tombstoning works
Tip• To test tombstoning on emulator
! & ?
Thank You ! Any Questions ?