locked
WinJS.Application.sessionState

    Question

  • Hi all,

    is there anything that could prevent the Application.sessionState from being restored correctly? I can see that the state is persisted correctly in the _sessionState.json in the LocalState directory of my apps package.

    But when the activation event handler is called, the sessionState object is empty and debugging later code reveals that it stays empty.

    It's a very straightforward mechanism so I'm somewhat stumped why it doesn't work...

    All the best

    Marcus

    Wednesday, March 28, 2012 3:55 PM

Answers

  • The sessionState object is only reloaded if the previousExecutionState of the app (see eventObject.detail.previousExecutionState) is equal to Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated, meaning that the app was suspended then terminated then relaunched. That's the scenario in which an app should bring itself back to its previous state, as opposed to other scenarios where the app should come up with its defaults.

    In other words, sessionState isn't designed for persistent storage between app launches in every scenario. If you want that, just maintain an object of your own and use the WinJS.Application.local.writeText/readText methods to save it (as you need to, with JSON.stringify) and load it on startup (with JSON.parse).

    You can look in the WinJS base.js file (search for sessionState) to see how sessionState is implemented, and pretty much do the same thing without the previousExecutionState condition.

    .Kraig

    Wednesday, March 28, 2012 4:26 PM

All replies

  • The sessionState object is only reloaded if the previousExecutionState of the app (see eventObject.detail.previousExecutionState) is equal to Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated, meaning that the app was suspended then terminated then relaunched. That's the scenario in which an app should bring itself back to its previous state, as opposed to other scenarios where the app should come up with its defaults.

    In other words, sessionState isn't designed for persistent storage between app launches in every scenario. If you want that, just maintain an object of your own and use the WinJS.Application.local.writeText/readText methods to save it (as you need to, with JSON.stringify) and load it on startup (with JSON.parse).

    You can look in the WinJS base.js file (search for sessionState) to see how sessionState is implemented, and pretty much do the same thing without the previousExecutionState condition.

    .Kraig

    Wednesday, March 28, 2012 4:26 PM
  • Hey Kraig,

    that's great to know! Do you have a tip how to debug this? Terminating the Debugger makes previousExecutionState to be NotRunning, manually closing the app results in ClosedByUser...

    Thursday, March 29, 2012 2:48 PM
  • Yes, there's a toolbar in Visual Studio with pause, play, and stop buttons (look for it on the second row when you're debugging, under the control that lets you select simulator/local machine). The pause button simulates suspend, play simulates resume, and stop simulated suspend then terminate. That should cause the app to restart with previousExecutionState==terminated in side the debugger.
    Thursday, March 29, 2012 3:05 PM