|
The graphic and related article for Windows 8 is adapted from Rachel Appel's article on The Windows Store App Lifecycle in the MSDN Magazine Special Edition on Windows 8 and
Windows Phone 7 Basics at Pluralsight
In this graphic, boxes represent states, words in bold are events handled by functions in file App.xaml.cs:
DO THIS: Long press on the back button for a list of tombstoned apps to select.
WP8 can hold in memory page navigation history for up to 8 (WP 7 can have up to 5) dormant apps (plus a cache of their state dictionaries).
But such state information can be disgarded if the device runs short of memory. Or in Visual Studio "Tombstone upon deactivation while debugging" is checked yes.
Even after an app has been closed, in Windows Phone 8 app can bypass going through Launch()
and to straight to Activated(), if
ActivationPolicy is (manually) added to the app's AppManifest.xml, as in:
Since the backstack (history from the previous instance) remains, clear it using this code:
So app code need to determine if state has been preserved or needs to be reloaded upon resumption:
Fast Application Resume (FAR)
<Tasks>
<DefaultTask Name="_default" NavigationPage="MainPage.xml" ActivationPolicy="Resume"/>
</Tasks>
private void IntializePhoneApplication(){
...
RootFrame.Navigated += CheckForResetNavigation;
...
}
private void CheckForResetNavigation(object sender, NavigationEventArgs e){
// If the app received a "reset" navigation, check on the next navigation
// to see if the page stack should be reset:
if(e.NavigationMode == NavigationMode.Reset)
RootFrame.Navigated += ClearBackStackAfterReset;
}
}
private void ClearBackStackAfterReset(object sender, NavigationEventArgs e){
// Unregister the event so it doesn't get called again:
RootFrame.Navigated += ClearBackStackAfterReset;
// Only clear the stack for new (forward) and refresh navigations:
if(e.NavigationMode != NavigationMode.New && e.NavigationMode != NavigationMode.Refresh )
Return;
}
while( RootFrame.RemoveBackEntry() != null) {} // do nothing.
}
Resume
private void Application_Activated(object sender, ActivatedEventArgs e){
if(e.IsApplicationInstancePreserved){
// Dormant - object in memory intact
}else{
// Tombstoned - need to reload
}
}
As with other object-oriented programs, each transition to another state invokes a defined function.
Under #region preservation methods service stores page state the user changes on the screen using function PreserveControlState defined for different control data types. function PreserverFocusState saves the control where user had established focus. The functions there are called from OnNavigatedFrom.
Under #region restoration methods function RestoreControlState is defined for different control data types to re-establish page state on the screen. The funcations there are called from OnNavigatedTo. But they are called only if IsNewPageInstance and IsStatePreserved.
To establish Application state during app start-up...
Among free MS Press books
An app is activated When the user presses the app's icon from the list of apps.
The entry point for the app is class [YourNameSpace].App. It inherits from the System class.
Silverlight apps handle Startup Event by consuming init args (InitParms properties) such as user name and permissions list.
However, there is currently no way to pass parameters into Windows Phone 7 apps.
On Launch, the Constructor from the app is invoked.
The coding to display the first screen is generated should not be touched:
QUESTION: What are the differences in the lifecycle within Windows 8 Store Apps and Windows Phone 8 or Windows Phone 7 apps?
Due to the limited amount of memory on mobile devices, all mobile operating systems (including Android, etc.) can automatically and completely remove running apps from operating memory, terminating their existance without notice.
This is an important departure from Windows 7 and before (which can have programs hang forever).
If Windows Phone 7 detects that not enough memory is available, it will invoke function
So transient data should be stored into a persistent media on the device.
When a user clicks the Home button, a phone call is received, the screen is rotated, etc. the operating system suspends the currently running app.
To keep media playing, detect idle condition with XML:
{Binding Path="RunUnderLockScreen".
Define a RunUnderLockScreen property.
PhoneApplicationService.Current.Disable Application Idle Detection Mode
App.CompleteInitializePhoneApplication
only have 10 seconds.
So should not save to a web service.
As with initial activation, the system calls:
However during reactivation:
Persistent data should be loaded into a transient state again.
when the user clicks a back button.
PhoneApplicationService.Closing
Appl.Application_Closing
Tombstoning.
WARNING: Don't position the cursor inside function definition code or it will stop there only when the if condition returns true.
Example:
function activated(e) { WinJS.UI.processAll().then(function () { // Navigate to either the first scenario or to the last running scenario // before suspension or termination var url = WinJS.Application.sessionState.lastUrl || scenarios[0].url; WinJS.Navigation.navigate(url); }); }
processAll() is not invoked until other activity occurs.
When the app is launched the activated event is raised after the DOMContentLoaded event and before the WinJS.Application.onloaded event. While the app is running, activated events may be raised at any time.
The e in activated(e) is short for an event object. If it returns true, the previous state of your app and to retrieve the activation parameters related to the contract that your app is being activated for. For a full list of the activation contracts and more details on their parameters see the ActivationKind enumeration.
This event can also be raised while your app is running if the system needs to pass the app parameters related to a new activation contract.
IMPORTANT FEATURE: Retrieve as part of activation the url from the sessionState stored before previous suspension or termination.
To recognize events such as when a button is clicked, controls need to be activated by invoking this code:
document.addEventListener("DOMContentLoaded", function (e){ WinJS.UI.processAll(); }
Other apps reference an activated() function defined earlier in the source file.
WinJS.Application.addEventListener("activated", activated, false); WinJS.Application.addEventListener("checkpoint", checkpoint, false);
ACRONYMN: PLM (Process Lifecycle Management)
More than just touch screens, Windows 8 brings to the desktop the behavior of Android phones in that it suspends and even terminate apps when it deems fit (to relieve memory pressure).
REFERENCE: How to activate an app
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |