Understanding
Windows Phone
7 application execution model, tombstone mechanism, starter and Selector and more
-- Part 1
Author:
Yochay kiriaty
The articles in this series have a name that is so long and very loud. I believe it will be very good. Of course you should judge it. However, the reason for such a long name is simple. These "big" words can briefly summarize all aspects of the execution model. If you want to continuously improve the user experience of applications, you should understand them. This article describes the execution model of a Windows Phone (WP) Application by starting, running, and disabling a Silverlight application. Note: For xNa games, these terms and concepts are the same, but there are slight differences in implementation. In subsequent articles, I will introduce deactivation, reactivation, and how the launcher and Selector work through the tombstone mechanism.
With the release of the latest beta version of the Windows Phone developer tool, applications running on Windows Phone use an updated execution model. Now, when your application is removed from the foreground (another application occupies the foreground, such as incoming calls or screen lock activated ), in fact, it will be terminated (soon ). In earlier versions of the Windows Phone tool, if you switch from one application to another, you may encounter various strange situations. This is because in the original version of the tool, the Windows Phone operating system does not completely terminate your program, but "pause" it. But now the question is, is it to start a new instance of an application or return it to the original program through the rollback stack? Even more confusing is that these related events and APIs are not well-developed. I think that's why we see the technology preview version-test and fix these "problems ".
Basic Rules
Make sure there is only one program. So far, you may know that the Windows Phone operating system does not allow third-party applications to run in the background. More specifically, only one application can run on the frontend at any time, which means that if your application is not displayed on the frontend, it will not run. The main reason third-party applications are not allowed to run in the background is to maintain battery endurance (see push notification service
) And make sure that a user experience can be quickly responded to and consistent.
All Windows Phone devices are equipped with a return button hardware. In addition to the return navigation function, this button also supports switching between applications. This is a very cool feature. Unlike other types of mobile phones, you can navigate from one application to a browser or another application, then press the return button to return to your application smoothly. This will provide a very unified user experience on different applications, regardless of whether it is a third-party application or a built-in app suite of mobile phones. This means that the Windows Phone operating system maintains a Directory for your application's behavior to support the return button function. This isRoll back Stack
.
Basic-start, execute, and close applications
It's time to look at the execution model of Windows Phone. The best way to do this is to study the code. Open a new Windows Phone application and view the code automatically generated by the program template of Windows Phone Silverlight.App. XAML. CS
The file finds four methods directly related to the execution model:
- Application _Launching
- Application _Activated
- Application _Deactivated
- Application _Closing
By the way, all these events (these methods are event handlers) areMicrosoft. Phone. Shell
NamespacePhoneapplicationservice
Class member.
I think the names of these methods are completely self-explanatory, but for the sake of insurance, let me explain it. I will talk about activated and deactivated events later. Now, let's look at two simple events: launching and closing events. I added the following line of code for each method (event handler:
// Step 1-util is helper class for logging <br/> util. trace ("***** in mainpage ctor/T (" + datetime. now. ticks + "*****)");
Util
Is a simple helper class that helps us debug. The text in each method varies according to the event, but you can also customize it. I noted down the time when each event occurred to prove it later when talking about activated and deactivated events.
Execute the program in the current status to obtain the expected results. Once the application starts the "Launching" method, it is called. As expected, after this event occurs, the program starts to run. Press the return button (not the Windows button) to terminate the program and the "Closing" event is triggered. As shown in (the output window is truncated from Visual Studio ).
As described in, by pressing the return button, the navigation model allows the user to "return to the previous step", return to the previous program page, or switch the application. However, once you return to the first page of the program (the first page, or the first instance of the Program), pressing the return button will triggerClosing
Event and terminate the program. You can clearly see that the program has exited. If you debug the code in Visual Studio as described in this article, you will notice that the program exits in the simulator and returns to the starting interface.
Understanding activated
, Deactivated
Event and tombstone mechanisms
So far, everything has gone smoothly. I mean there is no new content. Now let's take a look at the following content. Run your program in Visual Studio debug mode (we can see the stack information ). When the program is executed as expected, you will see the tracing information of application startup in the log. Now, instead of pressing the return button (if you press it, the program will be terminated), you will press the Windows button (the middle button ).
Note the following:
- When the simulator returns to the starting interface, your application disappears from the foreground, which means it is no longer running!
- The debugging session in Visual Studio is terminated. This indicates that Visual Studio no longer debugs the program because the program has stopped running.
Note:
Eventually, your program is terminated. We can easily see in the Visual Studio output window, just as the last line in the output window,The program '[ 220921918] taskhost.exe: managed'Has exited
With code 0 (0x0 ).
"But don't worry, this design is intended for it. Unlike the previous example, what is triggered after the launching event this time isDeactivated,
InsteadClosing
. However, in either case, the application will eventually be terminated. What exactly happened? What is the difference between closing and deactivated? More importantly, why is there such a difference?
- Application closed
(Application closing
)
Is the result of the user clicking the return button multiple times and navigating to the initial Page of the program. Now, the user exits an application.Unique
Channels. Once the application is closed, the associated process is terminated, and the operating system removes all information about the program from the memory (RAM.
- Application deactivated
)
This occurs when another different application obtains control of the foreground-for example, a call, start selector, or the user presses the Windows button. In these cases, your application will be disabled (Not close
). Before exploring the beauty of the deactivated event, we need to ensure that once it is understood that the program will eventually be terminated. Simple: Your Code cannot be executed in the background, so the program will terminate the execution. However, this is different from the exit of the program. A disabled program will be processed as a tombstone. Be sure not to confuse it. The process of a tombstone application has been terminated. However, unlike applications that have exited, Windows Phone clears all its stack information. However, after the application is disabled, the Windows Phone operating system stores the record of the current state of the application (tombstone ). Generally, the tombstone maintained by the Windows Phone operating system is part of the mobile app rollback stack. The rollback stack is a record that enhances the return button navigation function.
The application can be processed with a tombstone in the following ways. In the following cases, an application will be processed with a tombstone and disabled:
- The user presses the Windows button (as you did ).
- The screen is locked because the device times out (and another application takes control of the foreground ).
- The user has called the initiator or selector. We will introduce it in a later article.
At the current point of the application lifecycle (when your program is processed as a tombstone), we can perform some operations. You can return a tombstone program. This happens if the user executes the selector and initiator actions and returns the application, or if the user repeatedly presses the return key to return to a disabled (tombstone-based processing). Regardless of how the user returns to this program, the program will be re-activated (triggering the activated event) and the magic shows the last page that the user browsed before being deactivated.
If you don't believe it, let's try it. I added a page to the Basic Windows Phone Silverlight application and tracked the constructor of each page. Then, I added a button to navigate to the second page on the first page.
In Visual Studio, start debugging your program and click the button to navigate to the second page. On the second page, press the Windows button. As expected, the program is disabled and terminated, as you can see in the Visual Studio output window below.
Pay attention to the order of tracking information (and the corresponding time code ). First, the program starts, and the homepage constructor is called. After you press the button on the first page, the program will navigate to the second page (details page ), after you press the Windows button, the deactivated event is triggered, which indicates that the application is processed as a tombstone.
In the subsequent articles, you will learn how to return to an application processed by a tombstone and how to manage the status information of the application in the tombstone.
Sample Code can be downloaded here
This download.
Windows Phone 7 developer training kit
Contains a dedicated Windows Phone application Lifecycle
.
The msdn document also contains a topic Windows Phone execution model Overview
.
View the original text.
The purpose of this article is to learn and exchange, and the copyright is owned by the original author.