Handling Interruptions on Windows Phone

On Windows Phone, your game can be shut down at any time due to a number of events, such as:

  • A call, text message, or other top-level communication comes in, and the user decides to take it.
  • The user presses the phone's Home button while the game is running.
  • The user turns off the phone while the game is running.
  • The phone turns itself off because the user did not provide input within the system-wide timeout period.

To handle these interruptions, care must be taken to make sure that your game responds in a predictable way, and that game data is not lost. Applications that have been temporarily deactivated in this way are referred to as being tombstoned.

Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

When a Windows Phone game has been deactivated, there are two ways to save and restore data that should be considered:

  • The PhoneApplicationService.Current.State property, which will hold game data while the game is deactivated, but not closed. This is ideal for saving the game's current temporary (game paused) state data—for instance, the current positions and orientations of all objects on-screen).

    Since the PhoneApplicationService.Current.State's memory is lost if the game is closed, it's not a good place to save game data that will be needed to restore the long-term game state (the type of data typically stored when the game is saved).

  • The game's isolated storage space, which is ideal for data required to bring the game back from a complete restart. This is a good place to store data typically needed for a complete game save, but is not necessary for data that is not important in such a scenario (the state of in-game effects, for instance).

To save the game's temporary state on Windows Phone:

  1. Create event handlers for the PhoneApplicationService.Activated and PhoneApplicationService.Deactivated events. You should add your event handlers to the events in your game's initialization code, such as in the game class constructor:

            public Game1()
                PhoneApplicationService.Current.Activated += GameActivated;
                PhoneApplicationService.Current.Deactivated += GameDeactivated;
    To work with these events, you'll need to add the Microsoft.Phone assembly to your project's references. You can do this in Visual Studio by right-clicking References in Solution Explorer, and then clicking Add Reference.
  2. In the Deactivated event handler, store the game's temporary data in the PhoneApplicationService.Current.State property:

    void GameDeactivated(object sender, DeactivatedEventArgs e)
        PhoneApplicationService.Current.State["BugPos"] = bug.Position;
        PhoneApplicationService.Current.State["BugRot"] = bug.Rotation;
        PhoneApplicationService.Current.State["BugTarget"] = bug.Target;
        PhoneApplicationService.Current.State["BugMoving"] = bug.Moving;
        PhoneApplicationService.Current.State["foodLocations"] = foodLocations;
  3. In the Activated event handler, load the game's temporary data from the PhoneApplicationService.Current.State property:

    void GameActivated(object sender, ActivatedEventArgs e)
        bug.Position = (Vector2)(PhoneApplicationService.Current.State["BugPos"]);
        bug.Rotation = (float)(PhoneApplicationService.Current.State["BugRot"]);
        bug.Target = (Vector2)(PhoneApplicationService.Current.State["BugTarget"]);
        bug.Moving = (bool)(PhoneApplicationService.Current.State["BugMoving"]);
        foodLocations = (List<Vector2>)(PhoneApplicationService.Current.State["foodLocations"]);
        gameIsPaused = true;
        pausedImageOnScreen = false;

To save the game's persistent state on Windows Phone

To save the persistent game state follow the steps described on Writing Data (Windows Phone).

Testing Game Reactivation

Testing game reactivation allows you to determine if your deactivation/reactivation code and game save code is working correctly. You can test game reactivation using either the Windows Phone Emulator, or using a Windows Phone device.

To test game reactivation

  • Run your game in the debugger on the emulator or phone.

  • When the game is running, press the Home button on the emulator or phone.

  • Press the Back button on the emulator or phone to reactivate the game without relaunching it.

  • Ff827866.note(en-us,XNAGameStudio.41).gifNote
    If you re-launch the game by tapping its icon on the phone, or press F5 to restart debugging in Visual Studio, the game will not fire the PhoneApplicationService.Activated event.

    Best Practices for Handling Game Deactivation and Reactivation

    If the PhoneApplicationService.Activated event was fired (the game is being re-activated rather than restarted), you should bypass initial menus and start the game in a paused state (or to the mode or screen he or she was working with when the game was interrupted) using the temporary data stored in the PhoneApplicationService.Current.State property.

    If the game is not being re-activated, but re-started instead, you can start the player at the main menu, adding an option to restore the game to the last saved position.


    Topics in the Windows Phone Documentation

    Execution Model Overview for Windows Phone
    Under the Windows Phone execution model, applications are activated and deactivated dynamically when a user navigates away from the application. This document describes how to persist data correctly for a windows phone application when the application is activated or deactivated.
    Execution Model Best Practices for Windows Phone
    Windows Phone applications are terminated when the user navigates away from them. This topic highlights some best practices for handling execution model events.

    Community Additions