Search code examples
javascriptuwpwinjswindows-10-universalwindows-10-mobile

Windows 10 universal app not resuming from previous session


I have developed a windows 10 universal app using Html,css and JS. For allowing inline scripts i am using ms-appx-web context and has set ms-appx-web:///login.html as start page in manifest. Whenever I open my app in windows 10 mobile it works fine but if I switch to another app and then go to app again by selecting it from windows app list. Then it instead of resuming app from saved state it restarts it.

(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) {
            }
            if (WinJS.Application.sessionState.url) {
                localStorage.setItem("UserName", WinJS.Application.sessionState.name);
                window.location = WinJS.Application.sessionState.url;
            }
            args.setPromise(WinJS.UI.processAll().then(function () {
            }));
      }

    };

    app.oncheckpoint = function (args) {
        var location = window.location.href;
        var name = localStorage.getItem("UserName");
        WinJS.Application.sessionState.name = name;
        WinJS.Application.sessionState.url = location;
    };

    Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function (args) {
        if (WinJS.Application.sessionState) {
            window.location = WinJS.Application.sessionState.url;
            localStorage.setItem("UserName", WinJS.Application.sessionState.name);
        }
    }, false);
    Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", function (args) {
        var location = window.location.href;
        var name = localStorage.getItem("UserName");
        WinJS.Application.sessionState.name = name;
        WinJS.Application.sessionState.url = location;
    }, false);

    app.start();

})();

Can anyone suggest me what am I doing wrong?

I changed my app.onactivated event in main.js

    app.onactivated = function (args) {
    if (args.detail.kind === activation.ActivationKind.launch) {
        if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {

        } else {

        }
        args.setPromise(WinJS.UI.processAll());

        var name = Windows.Storage.ApplicationData.current.roamingSettings.values["name"];
        var url = Windows.Storage.ApplicationData.current.roamingSettings.values["url"];

        if (name) {
            localStorage.setItem("UserName", name);
        }
        if (url) {
            window.location.href = url;
        }
    }
};

But it stops running on window.location.href = url; line.

What i am trying to do is store username and current url on suspending event and want to restore it on resume event (when user opens app from app list which is already running.)


Solution

  • but if I switch to another app and then go to app again by selecting it from windows app list. Then it instead of resuming app from saved state it restarts it.

    I think you are not using Single-Page Navigation for your app.

    Please refer to Single-page navigation: the recommended model:

    The script context is destroyed and must be initialized again. The app might receive system events but not handle them because the script context is being destroyed and reinitialized.

    So the script context is already destroyed after you navigated to other page.

    To fix the problem, the best solution is to make your app a single paged application. And navigate pages using PageControl. You can refer to Quickstart: Using single-page navigation to get started.

    Update:

    but when I use window.location.href for redirecting in main.js it closes app.

    It's because you are using it in WinJS script. When you are leaving the page WinJS script context will be destroyed and thus executing the codes inside crash the app. To fix this you can use windows lifecycle API instead:

    var roaming=Windows.Storage.ApplicationData.current.roamingSettings;
    
    Windows.UI.WebUI.WebUIApplication.addEventListener("activated", function (args) {
        if (args.detail[0].kind === activation.ActivationKind.launch) {
            if (roaming.values["currentUri"]) {
                window.location.href = roaming.values["currentUri"];
            }
        }
    });
    
    Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", function (args) {
        roaming.values["currentUri"] = window.location.href;
        roaming.values["UserName"] = evt.srcElement.value;
        //save the other information of the page here
    });
    
    Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function (args) {
        var roam = Windows.Storage.ApplicationData.current.roamingSettings;
        if (roam) {
            if (roam["currentUri"])
            {
                window.location.href = roam["currentUri"];
            }
        }
    }, false);
    

    You can also refer to my demo.

    Notes: If you don't use WinJS at all, just remove the reference. Loading WinJS library on every page is not efficient.