导航:[首页]->[wingui]->[Windows 8学习笔记3 生命周期]

Reference

  1. Programming Windows 8 Apps with HTML, CSS, and JavaScript (Second Preview)
  2. http://code.msdn.microsoft.com/windowsapps/
  3. http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.viewmanagement.applicationviewstate

App Activation

(function () { 
    "use strict"; 

    var app = WinJS.Application; 
    var activation = Windows.ApplicationModel.Activation; 

    app.onactivated = function (args) { 
        if (args.detail.kind === activation.ActivationKind.launch) { 
            if (args.detail.previousExecutionState !==  
                activation.ApplicationExecutionState.terminated) { 
                // TODO: This application has been newly launched. Initialize  
                // your application here. 
            } else { 
                // TODO: This application has been reactivated from suspension.  
                // Restore application state here. 
            } 
            args.setPromise(WinJS.UI.processAll()); 
        } 
    }; 

    app.oncheckpoint = function (args) { 
    }; 

    app.start(); 
})();

If the activation kind is launch and the previous state is notrunning or closedByUser, the app should start up with its default UI and apply any persistent settings (such as those in its Settings panel). With closedByUser, there might be scenarios where the app should perform additional actions (such as updating cached data) after the user explicitly closed the app and left it closed for a while.

If the activation kind is launch and the previous state is terminated, the app should start up in the same state as when it was last suspended.

For launch and other activation kinds that include additional arguments or parameters (as with secondary tiles, toast notifications, and contracts), it should initialize itself to serve that purpose by using the additional parameters. The app might already be running, so it won’t necessarily initialize its default state again.

  1. args.detail.kind identifies the type of activation.
MemberValueDescription
Launch | launch0

The user launched the app or tapped a content tile.

Search | search1

The user wants to search with the app.

ShareTarget | shareTarget2

The app is activated as a target for share operations.

File | file3

An app launched a file whose file type this app is registered to handle.

Protocol | protocol4

An app launched a URL whose protocol this app is registered to handle.

FileOpenPicker | fileOpenPicker5

The user wants to pick files that are provided by the app.

FileSavePicker | fileSavePicker6

The user wants to save a file and selected the app as the location.

CachedFileUpdater | cachedFileUpdater7

The user wants to save a file that the app provides content management for.

ContactPicker | contactPicker8

The user wants to pick contacts.

Device | device9

The app handles AutoPlay.

PrintTaskSettings | printTaskSettings10

The app handles print tasks.

CameraSettings | cameraSettings11

The app captures photos or video from an attached camera.

PickerReturned | pickerReturned1000

Windows Phone only. The application was activated after the completion of a picker.

  1. args.detail.previousExecutionState identifies the state of the app prior to this activation, which determines whether to reload state.
MemberValueDescription
NotRunning | notRunning0

The app is not running.

Running | running1

The app is running.

Suspended | suspended2

The app is suspended.

Terminated | terminated3

The app was terminated after being suspended.

ClosedByUser | closedByUser4

The app was closed by the user.

App Suspending

When an app is not visible (in any view state), it will be suspended after five seconds to conserve battery power. This means it remains wholly in memory but won’t be scheduled for CPU time and thus won’t have network or disk activity (except when using specifically allowed background tasks). When this happens, the app receives the Windows.UI.WebUI.WebUIApplication.onsuspending event, which is also exposed through WinJS.Application.oncheckpoint. Apps must return from this event within the five-second period, or Windows will assume the app is hung and terminate it.

    function onSuspending(eventArgs) { /* Your code */ }
    // addEventListener syntax
    webUIApplication.addEventListener("suspending", onSuspending);
    webUIApplication.removeEventListener("suspending", onSuspending);
    // - or -
    webUIApplication.onsuspending = onSuspending;

    //////////////////

    WinJS.Application.addEventListener("checkpoint", listenerName);
    // - or -
    WinJS.Application.oncheckpoint = listenerName;

The best practice is actually to save session state incrementally (as it changes) to minimize the work needed within the suspending event, because you have only five seconds to do it.

App Resuming

If the user switches back to a suspended app, it receives the Windows.UI.WebUI.WebUIApplication.onresuming event.

    function onResuming(eventArgs) { /* Your code */ }

    // addEventListener syntax
    webUIApplication.addEventListener("resuming", onResuming);
    webUIApplication.removeEventListener("resuming", onResuming);
    // - or -
    webUIApplication.onresuming = onResuming;

App ViewState

MemberValueDescription
FullScreenLandscape | fullScreenLandscape0

The current app's view is in full-screen (has no snapped app adjacent to it), and has changed to landscape orientation.

Filled | filled1

The current app's view has been reduced to a partial screen view as the result of another app snapping.

Snapped | snapped2

The current app's view has been snapped.

FullScreenPortrait | fullScreenPortrait3

The current app's view is in full-screen (has no snapped app adjacent to it), and has changed to portrait orientation.