locked
Save session data - and load it again

    Question

  • So I'm still working on an app, and I have another problem. I use the navigation app template (pagecontrol). I can now definate some settings on my setting page (OptPage.js) and it will be loaded to all of the other pages, if I want to (Namespace).

    The problem is just that, right now does the user have to set the settings every time he opens the app. That's now fun for anyone. So I'm thinking about, how is it possible to save the settings, and open them Again, next time the app runs.

    You can see my code below. What I want is the metric.on to be saved and then loaded Again when the app starts up again. As soon as I navigate to the bmi page, should it again be loaded (proberly in the ready function).

    OptPage.js:

    ready: function (element, options) {
                // TODO: Initialize the page here.
    
                // Registrer eventHandler for 'bmiButton'
                var sumbitButton = document.getElementById("sumbitButton");
                sumbitButton.addEventListener("click", this.buttonClickHandler.bind(this), false);
            },
    
            unload: function () {
                // TODO: Respond to navigations away from this page.
            },
    
            updateLayout: function (element, viewState, lastViewState) {
                /// <param name="element" domElement="true" />
    
                // TODO: Respond to changes in viewState.
            },
    
            buttonClickHandler: function () {
                var inMe = document.getElementById("inMe");
                var selection = inMe.options[inMe.selectedIndex].value;
                var opt;
                var update = "Updated!"
                if (selection === "metric") {   // Metric
                    WinJS.Namespace.define(
                    "metric",
                    {
                        on: true
                    }
                    );
                }
                else {                          // Imperial
                    WinJS.Namespace.define(
                    "metric",
                    {
                        on: false
                    }
                    );
                }
                
                document.getElementById("sumbitOutput").innerText = update;
                // Below will make the option global 
                document.getElementById("divOption").innerHTML = metric.on;
            },

    Friday, February 22, 2013 5:50 PM

Answers

  • Whenever your metric.on value changes (in response to some action for example), do something like:

    //the line where you change the value to true or false
    metric.on=true;//or false
    //add this
    Windows.Storage.ApplicationData.current.roamingSettings.values["metric"] = metric.on;

    Do that everywhere you change the metric.on value. This will make sure that if the App closes anywhere, the latest value was stored.

    Then, on default.js, on the activated handler, you can initialize the namespace like:

    WinJS.Namespace.define(
    "metric",
    {
    	on: (Windows.Storage.ApplicationData.current.roamingSettings.values["metric"]!=null?Windows.Storage.ApplicationData.current.roamingSettings.values["metric"]:false)
    }
    );
    This will initialize metric.on to the last saved value or "false" in case it was never saved.

    • Marked as answer by Potices Wednesday, February 27, 2013 2:14 PM
    Tuesday, February 26, 2013 4:22 PM

All replies

  • Save the setting to ApplicationData.roamingSettings when the user changes it and read it in when the app is first launched. See Quickstart: Roaming app data for some sample code.

    --Rob

    Saturday, February 23, 2013 4:34 AM
    Owner
  • I'm pretty confused about that. I don't know where to write it, and what to write at all?
    Saturday, February 23, 2013 7:28 AM
  • I don't know if I explained in a wrong way, but nobody is helping, so I'll give more information:

    I've really been trying hard to understand this ApplicationData.roamingSettings, but I think it's very very hard for me, cause I'm still new at app development.

    I don't know if I just can save the appdata when the user press the button (

    buttonClickHandler: function 
    ) but Again, I'm really really confused. So if somebody could help would I be very very glad.
    Sunday, February 24, 2013 6:11 PM
  • Hi Potices,

    I would like to suggest you save state using roaming storage on button click event and retrieve it in ready function.

    at first, declare onVar session and assign value for it on button click event, as following

     var inMe = document.getElementById("inMe");
            var selection = inMe.options[inMe.selectedIndex].value;
            var onVar;
    
                if (selection === "metric") {   // Metric
                    onVar = "true";
                }
                else {                          // Imperial
                    onVar = "false";
                }
    
                // Save the session data. 
                WinJS.Application.sessionState.onVar = onVar;
    
                // Below will make the option global 
                document.getElementById("divOption").innerHTML = onVar;//metric.on;

    and then place the retrieve onVar session state code before registrer eventHandler for 'bmiButton', as following:

     // Retrieve our onVar session state info if it exists. 
                var outputValue = WinJS.Application.sessionState.onVar;
                if (outputValue) {
                    document.getElementById("divOption").innerHTML = outputValue;
                }
    if you want to declare the session state as global variable, you can write it like

     WinJS.Namespace.define(
                   
    "metric",
                   
    {
                        on
    : WinJS.Application.sessionState.onVar;
                   
    }
    Hope this helps, thanks.


    Yanping Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 25, 2013 7:07 AM
    Moderator
  • I havn't tried it yet, but when I look at your answer, it doesn't look like you understood the question.

    What I want is save the userdata (only the metric.on /metric.onVar), so everytime the user opens the app, they don't have to set the metric.on .

    Cause right now is it possible to change the setting, but when they close the app, is it just back to the default setting.

    Tuesday, February 26, 2013 6:28 AM
  • If you save the setting in roamingSettings then it will persist. You can read it from roamingSettings even after the app has exited and restarted, and it will roam between machines so the user can set the setting at one machine and it will still be set on another.

    You need to make sure you don't override the setting by initializing it to a default value when it has already been set.

    I wouldn't recommend using sessionState for settings. It is more appropriate for transitory state such as the current navigation state. It is designed so that if the app is suspended the user will come back to the same state when resumed. SessionState may not persist if the app doesn't suspend property.

    --Rob 

    Tuesday, February 26, 2013 7:24 AM
    Owner
  • So how and where should I set the roamingSettings? And how to read it?

    Tuesday, February 26, 2013 3:42 PM
  • Whenever your metric.on value changes (in response to some action for example), do something like:

    //the line where you change the value to true or false
    metric.on=true;//or false
    //add this
    Windows.Storage.ApplicationData.current.roamingSettings.values["metric"] = metric.on;

    Do that everywhere you change the metric.on value. This will make sure that if the App closes anywhere, the latest value was stored.

    Then, on default.js, on the activated handler, you can initialize the namespace like:

    WinJS.Namespace.define(
    "metric",
    {
    	on: (Windows.Storage.ApplicationData.current.roamingSettings.values["metric"]!=null?Windows.Storage.ApplicationData.current.roamingSettings.values["metric"]:false)
    }
    );
    This will initialize metric.on to the last saved value or "false" in case it was never saved.

    • Marked as answer by Potices Wednesday, February 27, 2013 2:14 PM
    Tuesday, February 26, 2013 4:22 PM