locked
Registering events for application lifecycle RRS feed

  • Question


  • I am trying to understand the proper pattern for making an application that can handle suspend/resume events.  The single page template and the sample for App activating have differences that I don't follow.
    The template uses
     WinJS.Application.onmainwindowactivated = function (e) {
                    if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
    


    and WinJS.Application.Start();

     

    but the sample for App activating and suspending events uses
    Windows.UI.WebUI.WebUIApplication.addEventListener("activated", activatedHandler, false); 
    
     function activatedHandler(eventArgs) {
            if (eventArgs.kind == Windows.ApplicationModel.Activation.ActivationKind.launch) { 
    
    
     with no WinJS.Application.Start();

     

    Are they equivalent?  I don't think so.  If one follows the template method, how to register and add event handlers for activating/suspend/resume?

     

    Monday, October 31, 2011 1:46 PM

Answers

  • Hi JR,

    The cool thing is that you have the source code to answer this question yourself!  I will walk you through this so we help others in the community solve any similar issues or questions.

    The WinJS code is contained in the winjs\js directories that are included when you build an app from the application templates (wizard).

    Application stuff is contained in the wwaapp.js file.  If you look through there and search for: mainwindowactivated (the event handler from the template is onmainwindowactivated) you will see it is defined as: mainwindowactivatedET.  Searching THAT you will see this code:

    var wui = Windows.UI.WebUI.WebUIApplication;
            wui.addEventListener("activated", function (e) {
                WinJS.Application._loadState(e).then(function () {
                    queueEvent({type: mainwindowactivatedET, detail: e});
                });

     

    So indeed winjs is defining the same function for you as the simple sample is and it is equivalent!

    -Jeff

     


    Jeff Sanders (MSFT)
    • Marked as answer by jrboddie Monday, October 31, 2011 6:31 PM
    Monday, October 31, 2011 3:57 PM
    Moderator
  • To expand a little on Jeff's answer, there is a little difference between just adding event listeners and using the WinJS application model. WinJS queues the various events so that they're not fired until WinJS.Application.start() is called. The idea here is that you can be sure that your initialization code has been executed before any of those events occur. As stated in //Build session 501, it means you won't miss any.

    The WinJS model essentially translates some of the standard HTML events into what might be called a more normalized series. As I understand it, WinJS is looking to provide a structure on top of a broader series of events to simplify the sequencing and thus the programming model.

    Anyway, in the comments of wwaapp.js (part of WinJS), you see the sequence:

    // loaded == DOMContentLoaded
    // mainwindowactivated == after WinRT Activated
    // ready == after all of the above

    WinJS has built-in listeners for some of these events, for its own internal processing, but makes sure that those listeners gets called last in the chain.

    So as Jeff said, mainwindowactivated is the same as Windows.UI.WebUI.WebUIApplication.addEventListener("activated"). Also, in WinJS the "suspending" event from WinRT is translated into the "checkpoint" event. But there isn't an equivalent for "resuming" so you'd need to listen to that one specifically.

    .Kraig

     

    • Marked as answer by jrboddie Monday, October 31, 2011 6:31 PM
    Monday, October 31, 2011 4:38 PM

All replies

  • Hi JR,

    The cool thing is that you have the source code to answer this question yourself!  I will walk you through this so we help others in the community solve any similar issues or questions.

    The WinJS code is contained in the winjs\js directories that are included when you build an app from the application templates (wizard).

    Application stuff is contained in the wwaapp.js file.  If you look through there and search for: mainwindowactivated (the event handler from the template is onmainwindowactivated) you will see it is defined as: mainwindowactivatedET.  Searching THAT you will see this code:

    var wui = Windows.UI.WebUI.WebUIApplication;
            wui.addEventListener("activated", function (e) {
                WinJS.Application._loadState(e).then(function () {
                    queueEvent({type: mainwindowactivatedET, detail: e});
                });

     

    So indeed winjs is defining the same function for you as the simple sample is and it is equivalent!

    -Jeff

     


    Jeff Sanders (MSFT)
    • Marked as answer by jrboddie Monday, October 31, 2011 6:31 PM
    Monday, October 31, 2011 3:57 PM
    Moderator
  • To expand a little on Jeff's answer, there is a little difference between just adding event listeners and using the WinJS application model. WinJS queues the various events so that they're not fired until WinJS.Application.start() is called. The idea here is that you can be sure that your initialization code has been executed before any of those events occur. As stated in //Build session 501, it means you won't miss any.

    The WinJS model essentially translates some of the standard HTML events into what might be called a more normalized series. As I understand it, WinJS is looking to provide a structure on top of a broader series of events to simplify the sequencing and thus the programming model.

    Anyway, in the comments of wwaapp.js (part of WinJS), you see the sequence:

    // loaded == DOMContentLoaded
    // mainwindowactivated == after WinRT Activated
    // ready == after all of the above

    WinJS has built-in listeners for some of these events, for its own internal processing, but makes sure that those listeners gets called last in the chain.

    So as Jeff said, mainwindowactivated is the same as Windows.UI.WebUI.WebUIApplication.addEventListener("activated"). Also, in WinJS the "suspending" event from WinRT is translated into the "checkpoint" event. But there isn't an equivalent for "resuming" so you'd need to listen to that one specifically.

    .Kraig

     

    • Marked as answer by jrboddie Monday, October 31, 2011 6:31 PM
    Monday, October 31, 2011 4:38 PM
  • Jeff and Kraig,

    Thanks for the detailed explanation.  JavaScript examples are sometimes confusing to me when there are multiple ways of getting to the same result.  I know that initialization of things correctly is important to avoid race conditions that may seem to work under some situations but fail in others.

    ***Edit: I just watched Session 501.  It cleared up many questions for me.  Thanks.
    • Edited by jrboddie Tuesday, November 1, 2011 2:39 AM Additional comment
    Monday, October 31, 2011 6:38 PM