locked
Template code for Basic JavaScript App RRS feed

  • Question

  • In default.js for the Basic template, we find this code:

        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());
            }
        };

    If I read this right is says that if the app is launches and the launch type isn't terminated, then we should assume that the app has never been run before, and initialize it. Otherwise, we should resume from suspend.

    Is that really best practice? Shouldn't we refresh if resumed, and restore state (if available) otherwise?

    S


    Check out my new C# 2010 All In One for Dummies book at Amazon!

    Saturday, July 14, 2012 7:55 PM

Answers

  • Terminated indicates that your application was suspended by the system, so you should restore your previous *session* state, if any. 

    Otherwise, it doesn't mean that it has never been run before, just that it isn't coming back from being suspended (e.g. perhaps the user just restarted their machine and launching your app for the first time post-reboot, but they used you prior to the reboot as well).  What it is saying is in that first part you should do whatever you need to do when the user launched your app cleanly - if you stored local state from a previous usage session, you can of course restore that (e.g. last high score, etc.), while handling yourself the case of it having never been launched.  In most cases this would mean the users re-enters your app at the beginning/first page.

    In the second section (terminated === true) then you'd want to restore the session's state - e.g. the user comes back at the exact same place they were before as if they had never left.

    • Marked as answer by Bill SempfMVP Tuesday, July 17, 2012 11:16 AM
    Saturday, July 14, 2012 10:11 PM

All replies

  • Terminated indicates that your application was suspended by the system, so you should restore your previous *session* state, if any. 

    Otherwise, it doesn't mean that it has never been run before, just that it isn't coming back from being suspended (e.g. perhaps the user just restarted their machine and launching your app for the first time post-reboot, but they used you prior to the reboot as well).  What it is saying is in that first part you should do whatever you need to do when the user launched your app cleanly - if you stored local state from a previous usage session, you can of course restore that (e.g. last high score, etc.), while handling yourself the case of it having never been launched.  In most cases this would mean the users re-enters your app at the beginning/first page.

    In the second section (terminated === true) then you'd want to restore the session's state - e.g. the user comes back at the exact same place they were before as if they had never left.

    • Marked as answer by Bill SempfMVP Tuesday, July 17, 2012 11:16 AM
    Saturday, July 14, 2012 10:11 PM
  • I might buy that.

    But if terminated indicates that the app was suspended, what does Windows.ApplicationModel.Activation.ApplicationExecutionState.suspended indicate?

    S


    Check out my new C# 2010 All In One for Dummies book at Amazon!

    Sunday, July 15, 2012 1:39 AM
  • Hi,

    Windows.ApplicationModel.Activation.ApplicationExecutionState.suspended means the app comes back from the suspended state. In most cases, we don't need to do anything, as the app state is persisted in memory.

    In addition, you can find more details about those descriptions on http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.activation.applicationexecutionstate.aspx.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Monday, July 16, 2012 5:12 PM
    Moderator