Windows 8: The App Lifecycle

Excerpt by Ken Getz | November 01, 2012

You may have noticed that none of the built-in Windows 8 applications provide any means of shutting down. As a matter of fact, you should never include a means of shutting down a Windows 8 application-instead, you should allow Windows 8 to manage the life cycle of your application for you.

Windows 8 can load and run multiple applications, just like prior versions of Windows. Windows 8, however, uses intelligent management of apps, loading and unloading the applications as needed to conserve memory and battery power. You can switch between loaded apps, and you'll never notice that Windows 8 may have suspended or terminated the application for you. You don't need to worry about slowing down the system, or about battery drain. You don't have a need to manually "kill" apps (unless you're currently developing the app, of course-then you must be able to shut it down in order to modify and rebuild it).

As part of its management of applications, Windows 8 automatically suspends (and terminates, if necessary) applications. As you switch away from an application, Windows will suspend it after a period of time, restoring it immediately if you switch back to the application. In addition, Windows may terminate an application (if, perhaps, it can't suspend within the allotted 5-second time frame). Well-designed applications can be suspended, terminated, and restarted and appear as if they were running the whole time. That's the challenge for developers-making sure their applications respond to requests to be suspended by saving their state, so that they can be restored without loss of data.

WARNING! Although Windows 8 notifies your applications as it's about to suspend the application, it does not notify applications when they're being terminated. If you intend to save the state of your application, your code should include code to do so in the application's OnSuspending override-that's the only chance you get to save the application's state.

Examine the App Lifecycle

It's important to understand the states in any application's lifecycle. Note these facts about the lifecycle of a Windows 8 application:

  • Activation marks the beginning of the app's lifecycle.
  • At any point, an app can be running, suspended, or not running. It could also be in a transition state, either suspending or resuming.
  • Applications can be suspended when you switch away from them, to another application. They can also be suspended when Windows enters a low power state.
  • Suspended applications reside in memory, which allows for a quick resume.
  • When an app is suspended, it might be resumed at any time. Perhaps the user switches back to the application; it looks as if the application was running the entire time, to the user.
  • When terminated, an application stops running and is removed from memory.

Figure 1 diagrams the states and transitions for an application running Windows 8.

Ken Getz

Ken Getz is a featured instructor for several of our Visual Studio courses. He is a Visual Basic and Visual C# expert and has been recognized multiple times as a Microsoft MVP. Ken is a seasoned instructor, successful consultant, and the author or co-author of several best-selling books. He is a frequent speaker at technical conferences like Tech-Ed, VSLive, and DevConnections and he has written for several of the industry's most-respected publications including Visual Studio Magazine, CoDe Magazine, and MSDN Magazine.

This course excerpt was originally posted November 01, 2012 from the online courseware Windows 8 Using XAML, Part 01: Introduction to Apps by Ken Getz