Windows Phone Mango – Tombstoning XNA

It struck me that tombstoning when building a XNA application has to be somewhat different then tombstoning a regular Windows Phone Silverlight application. I was thinking of the “Pause screen”, Guide Dialogs (these have to halt the game in some way) etc. Also the events are triggered at different occasions in an XNA application then in a Silverlight application. In Windows Phone Mango you can also mix Silverlight/XNA, are there any special actions needed for application life cycle and tombstoning in these cases?

Let’s go through how tombstoning in XNA applications work and what the differences are compared to a regular Windows Phone Silverlight application.

As I wrote in my previous post Windows Phone Silverlight applications has four events

  • Launching
  • Activated
  • Deactivated
  • Closing

XNA also has four methods you can override in the Game class to trigger the events

  • Initialize
  • OnActivation
  • OnDeactivation
  • OnExiting

So from a first look it seems like they are corresponding to each other, although the events are not called in the same way and order. The following images shows how the XNA specific events are called.

We can see that the Initialize method is executed when the application is started but also when the application is re-activated from a tombstoned state. Also the OnActivation method is called even when application starts for the first time, as well as the OnDeactivation method is executed when the application exists. This basically means that we cannot use the OnActivation and OnDeactivation methods to check if the XNA application was resumed from a tombstoned state.

So with Mango this all gets even more confusing and problematic. When the user navigates away from an XNA application it is put in a dormant state and it does not get tombstoned directly. This means that when the user navigates away, in Mango only the deactivated event is used, and Exiting method will never be called (this method was called in pre-mango versions) even if the application is tombstoned later. But there is a good explaination for this behavior, these events are not a specific feature in Windows Phone, they are used to provide notifications when the game gains and loses focus. If you think about it, there is no concept of tombstoning and dormant state in a PC or XBox XNA game. So what you have to do, and should do is to use the PhoneApplicationService class in your XNA games as well. And with Mango version they are actually required to be able the detect the dormant state.

Below is a table that summarizes the methods and when they are executed in a Mango XNA game

Initialize OnActivation OnDeactivation Exiting
Game Launches x x  
Game is deactivated (tombstoned)   x x
Game is deactivated (dormant) x
Game is reactivated x x  
Guide dialog is shown  x
Guide dialog is closed x
Game is exited   x x

PhoneApplicationService class

As mentioned above you need to use the PhoneApplicationService class to properly handle Dormant state in a Mango XNA game.

To use the PhoneApplicationService class you have to add a reference to Microsoft.Phone assembly, this is totally legitimate thing to do, and as said before required if you want to (and should) handle  Dormant states for fast app switching and good user experience. We can then add event handlers for the four events that are executed in a same way as a regular Windows Phone Silverlight application.

PhoneApplicationService.Current.Launching += PhoneApplicationService_Launching;
PhoneApplicationService.Current.Activated += PhoneApplicationService_Activated;
PhoneApplicationService.Current.Deactivated += PhoneApplicationService_Deactivated;
PhoneApplicationService.Current.Closing += PhoneApplicationService_Closing;

Handle these events in the same way as described in my previous post about Windows Phone Mango – Tombstoning and life cycle.

Silverlight/XNA application

In Mango RTM there is a possibility to create a new project called “Windows Phone Silverlight and XNA application”. If you create a project of this template you will already have a reference to the Microsoft.Phone assembly and corresponding event handlers for handling dormant, tombstoned state and application life cycle. See my previous post about Mango tombstoning and life cycle.


Hopefully this has cleared some of your concerns about life cycle in a Mango XNA application. You should think about the Game class as something that is XNA specific with events basically used to detect when the game gains and loses focus. You should then use the PhoneApplicationService class to properly handle life cycle, tombstoning and dormant states. This includes saving transient and persisting data.

Happy Coding!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: