0

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.)

Sonali
  • 2,223
  • 6
  • 32
  • 69
  • If you are using `ms-appx-web:///` in your application, `Windows` namespace is not even available. I'm curious how can you run your app without any error. Could you please share a basic demo that can reproduce your problem? – Elvis Xia - MSFT Sep 08 '16 at 06:32
  • I have added ApplicationContentUriRules in manifestfile ` ` – Sonali Sep 08 '16 at 07:10
  • I only want to resume my app from where it was left. without using WinJS.Navigation.Navigate. – Sonali Sep 08 '16 at 08:45
  • I think you need to make your app a single paged application, otherwise the javascript context will be destroyed. You can choose not to use WinJS, use other framework instead. ex: [angular-ui-router](https://github.com/angular-ui/ui-router/tree/legacy). – Elvis Xia - MSFT Sep 08 '16 at 08:56
  • I am developing Windows 10 Universal App...any other workaround – Sonali Sep 08 '16 at 09:35
  • An ugly and inefficient workaround is to reference the WinJS on every page of your app. And add suspend event handler on every page's JS codes. In every page's suspend event hander save current page's url to local file. Then read it on every start of your app. But really not recommended way. – Elvis Xia - MSFT Sep 08 '16 at 10:00
  • but when I use window.location.href for redirecting in main.js it closes app. it is not redirecting to last url – Sonali Sep 08 '16 at 10:03
  • `app.oncheckpoint = function (args) { Windows.Storage.ApplicationData.current.roamingSettings.values["name"] = localStorage.getItem("UserName"); Windows.Storage.ApplicationData.current.roamingSettings.values["history"] = window.location.href; };` – Sonali Sep 08 '16 at 10:10
  • `app.onactivated = function (args) { if (args.detail.kind === activation.ActivationKind.launch) { args.setPromise(WinJS.UI.processAll().then(function () { if (Windows.Storage.ApplicationData.current.roamingSettings.values.hasKey("history")) { localStorage.setItem("UserName", Windows.Storage.ApplicationData.current.roamingSettings.values["name"]); window.location.href = Windows.Storage.ApplicationData.current.roamingSettings.values["history"]; } })); } };` – Sonali Sep 08 '16 at 10:11
  • I've updated my answer, please have a check. – Elvis Xia - MSFT Sep 09 '16 at 02:20
  • @ElvisXia-MSFT Can you help me with this? [Windows App Certification Failed Universal Windows App 10](http://stackoverflow.com/questions/39492921/windows-app-certification-failed-universal-windows-app-10) – Sonali Sep 14 '16 at 14:32

2 Answers2

0

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.

Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • How will I bind my back button event ? – Sonali Sep 09 '16 at 03:56
  • So what will be the Replacement for `app.onbackclick = function (args) { if(window.location.href.includes("login.html")) { app.stop(); } else { window.history.back(); return true; } };` – Sonali Sep 09 '16 at 04:51
  • `var hardwareButtons = Windows.Phone.UI.Input.HardwareButtons; hardwareButtons.addEventListener("backpressed", function (e) { //to do });` I used this. – Sonali Sep 09 '16 at 05:19
  • Windows.Phone.UI.Input.HardwareButtons; showing error in Windows desktop app. What to do? – Sonali Sep 09 '16 at 06:28
  • This API is only available for Windows Phone. Please refer to [HardwareButtons](https://msdn.microsoft.com/en-us/library/windows/apps/windows.phone.ui.input.hardwarebuttons.aspx?f=255&MSPPError=-2147217396) – Elvis Xia - MSFT Sep 09 '16 at 06:31
  • Maybe you need to ask another question with some detail codes there. – Elvis Xia - MSFT Sep 09 '16 at 06:39
  • I want to know that how I can now add phone back button(key press) event in current scenario?while not using winjs – Sonali Sep 09 '16 at 06:40
  • You can make that work when it runs on a windows phone or a windows phone emulator. – Elvis Xia - MSFT Sep 09 '16 at 06:42
  • But not on a desktop. – Elvis Xia - MSFT Sep 09 '16 at 06:42
  • How? without using winjs...Can you give any sample code? I want to add back key press event without having any error in my desktop app? – Sonali Sep 09 '16 at 06:43
  • This API is not depended on WinJS. You can use it without WinJS. If you want to ask how to use it. I really suggest you ask a new question about it. It is not this case related. – Elvis Xia - MSFT Sep 09 '16 at 06:59
  • I already did : http://stackoverflow.com/questions/39405480/how-to-add-back-button-event-in-universal-windows-app-without-using-winjs-librar – Sonali Sep 09 '16 at 07:01
  • I've answered that, please have a check. – Elvis Xia - MSFT Sep 09 '16 at 07:20
0

I have changed my main.js as :

    (function () {
"use strict";

//No need of WinJS
var activation = Windows.ApplicationModel.Activation;
var roaming = Windows.Storage.ApplicationData.current.roamingSettings;

// For App Start Up
Windows.UI.WebUI.WebUIApplication.addEventListener("activated", function (args) {
    if (args.detail[0].kind === activation.ActivationKind.launch) {
        if (roaming.values["currentUri"]) {
            if (roaming.values["UserName"])
            {
                localStorage.setItem("UserName", roaming.values["UserName"]);
                window.location.href = roaming.values["currentUri"];
            }
        }
    }
});

// For App Suspension
Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", function (args) {
    roaming.values["currentUri"] = window.location.href;
    roaming.values["UserName"] = localStorage.getItem("UserName");
});

// For Resuming App
Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function (args) {
    var roam = Windows.Storage.ApplicationData.current.roamingSettings;
    if (roam) {
        if (roam.values["currentUri"]) {
            localStorage.setItem("UserName", roam.values["UserName"]);
            window.location.href = roam.values["currentUri"];
        }
    }
}, false);
// not working backpressed event
Windows.UI.WebUI.WebUIApplication.addEventListener("backpressed", function (args) {
   // to do
}, false);})();

Everything is working fine. But I dont know how to add back key press event without using winjs. Can anyone suggest me? How to add back key press event without winjs?

Community
  • 1
  • 1
Sonali
  • 2,223
  • 6
  • 32
  • 69