How’s that for a title. At least it’s descriptive. As it says, this post was going to be on Launchers and Choosers in Windows Phone 7. After struggling a bit I realized that before I could understand exactly how the Choosers were working, I needed to understand the application execution model. So here we are, the WP7 application execution model.
Every post I’ve read about the WP7 execution model starts out talking about the lack of multi-tasking and this post will be no different. WP7 does not support multi-tasking. This point has caused some controversy. Old versions of the Windows phone supported multi-tasking. So does Android and iPhone (with iOS4). The rest of this paragraph is my opinion on the multi-tasking issue. Unless MS can get multi-tasking down perfect, I’m glad it is not included. So why doesn’t MS just copy Android’s model? Because it sucks. If my Mom has an Android phone and I have to explain to her how to kill an application because it’s draining her battery, then the model is broken. I believe Steve Jobs said something like, “If you have to manually kill an application, you are doing it wrong.” As soon as I see some flavor of a task manager on a phone I cringe. I don’t want WP7 to be a geek only phone. I want it in as many hands as possible, and I want it to just work. So why not follow Apples model. They have done a better job. It’s simple. As a user I don’t have to worry about closing an app. That said, since I’ve upgraded my iPhone 3gs to iOS4, it’s been much more buggy then it used to be. In my naive mind, I blame this on the multi-tasking. I have seen the phone become more responsive by going through my app history and clearing it out. If I have to do that, it’s the same as a task manager, it just looks nicer. I may be the only one, but until MS can get mutli-tasking perfected, I don’t want to see it on the phone. That’s my opinion, take it for what it’s worth. Let’s get into the details of what this means.
Without multi-tasking this means that any time your app is not on the screen, it is terminated. There are lots of reasons for your application to leave the screen. A few examples include the user hitting the back button beyond the start of your application, hitting the windows, using a chooser to select a phone number from a list of contacts, etc. All of these examples will terminate your application. There are four important methods that fire as your application loads and is terminated. Each of these methods are defined in the App.xaml.cs class of your application. Let’s look at each one and understand what they do.
Application_Launching - The launching method fires when you application loads a new instance. This is the method that would fire the first time you run your app. Also, if you hit the Windows button, go to the application list and launch your application this method will fire. This method indicates that you are launching a brand new instance of your application.
Application_Activated - Activated and launching are mutually exclusive. Both will not execute, only one. Activated is run whenyour application is loaded, but it’s not a new instance. This was confusing to me, because instance isn’t really the correct word. The applications are always a new “instance” because there is no multi-tasking. The difference is how your application is launched. If activated is called, it means that your app was running, then was terminated to do something else, and then was launched again. An example of this would be using the Windows key, launching a different application, then using the back button until your application is on the screen again. A better example is when you use a chooser to select a phone number. When the chooser returns with a result, application activated will be called.
Application_Deactivated – The opposite of Activated, Deactivated is called when you might come back to the application. If a phone call comes in or if you launch a chooser to select an email address deactivated would be called.
Application_Closing - Closing happens when you aren’t going to come back to your application. Mainly this occurs when you use the back button to go past the start of your application.
I can’t stress enough that any time your application is not on the screen, it is terminated. You can use these events to handle the storage and loading of data when this happens. There are several ways to store data but two very common ones. One is in IsolatedStorage. You should use IsolatedStorage any time you need to store data more permanently. The other common option is in the PhoneApplicationService.Current.State dictionary. Use the dictionary when it’s nice if the data was stored, but not critical. A common use here is to save the values from a form that the user entered so that when they come back, they don’t have to start over with a blank screen. Keep in mind that your data is not guaranteed to remain in the State dictionary.
If you want to learn more about the execution model there is a great three part post from Yochay. Here are the links: Part 1, Part 2, Part 3.
My next blog post will be on Launchers and Choosers. Before I get to that, here’s a quick tip if you are getting started with them. When you launch your chooser and then see a blank screen after you pick something, hit F5 in Visual Studio. This is a feature. Notice that the VS debugger detaches when you launch the chooser. The blank screen is your opening to re-attach the debugger so you can continue debugging your app. It took me a while to figure this out so hopefully this helps.