If you hadn’t already noticed Microsoft have released the first beta of the Windows Phone 7 developer tools. There are some significant, breaking, changes from the previous release from April. These have been quite well documented in this migration post however there are some gotchas that aren’t pointed out. The first of these is what happens when your application goes into the background.
As most of you will be aware Microsoft has been avoiding the question as to what the behaviour is when your application goes into the background (eg the Start is displayed and/or another application loaded). This is in part because they don’t want developers to develop according to the current Windows Phone implementation. Rather they want developer to develop against a contract which states that “should your application go into the background, it should assume that it will be terminated at any point in the future by the operating system”. What this means in practice is that Windows Phone has the option of leaving your application in memory, effectively running in the background, or terminating it immediately or at a later stage.
Let’s take a look at what happened in the April CTP – in this case when your application went into the background there was a Paused event raised on the Application which you could intercept and persist any information about the current state of your application. However, your application wasn’t actually terminated when the user presses the Start button (on the emulator that is).
Now, let’s look at the behaviour in the beta of the tools. Now, when you run your application, then click on the Start button you will notice that your application is terminated (this is evident because you will notice Visual Studio is no longer attached to the running process). However, when you click the Back button, your application should reappear, right? Wrong! Well, at least, without you doing anything that will be the case. What you actually see when you press the Back button is a blank screen. At this point if you run your application from Visual Studio 2010 it will start as if the user had pressed the Back button.
Take the following scenario:
1) Application loads and displays page 1
2) User navigates to page 2
3) User clicks Start (application is terminated)
4) User clicks Back (application is not running)
>> Press F5 or Run in Visual Studio
5) Application loads and returns to page 2.
Note that when the application loads for the second time, page 1 is never loaded. However, if the user presses the Back button again page 1 will again be displayed.
You should also be aware of the events that get raised within your application (these are exposed by the PhoneApplicationService and are wired up by default in App.xaml to methods in the App.xaml.cs file)
Launching – This event is raised when the application is launched from the Start screen. This event is not raised when the application is reloaded when the Back button is pressed.
Activated – This event is raised when the application is returned to by clicking the Back button. This event is not raised when the application is launched from the Start screen
Deactivated – This event is raised when the application is sent into the background. This might be due to the user clicking the Start button, or launching a task. You should save both persistent (between application sessions) and transient (for the current application session) at this point. You should assume that your application may get terminated at will by the operating system. You will not get another opportunity when this happens to persist information.
Closing – This event is raised when the application is closed by the user pressing the Back button when at the home/first page of the application.
I can’t reiterate this enough: When you get the Deactivated event, make sure you save both the persistent and transient state of your application. This should also include information about the visual state of the current page (eg what text the user may have entered, perhaps even which control has focus). When the user clicks the Back button, they will want to see the same page that was there when they pressed the Start button, irrespective of whether the application was terminated and relaunched, or not.