[WinJS, WinRT] Maintain app state without losing any data in WinJS apps RRS feed

  • Question

  • I have a very complex Windows 8.1 app that is built under HTML/JavaScript/CSS (i.e. WinJS) with Cordova/PhoneGap and necessary Windows Runtime Component for native operations like web-service calls, SQLite, etc.

    I have another app which is specially built for this WinJS project that is developed completely using C# and XAML.

    My requirement is to open this second app from the first app and retrieve data from second app and pass it back to the first app. I have managed to open second app from first app and pass data between those apps using custom protocols.

    await Launcher.LaunchUriAsync(new Uri("secondapp://somedata"));

    In my first app (WinJS app), many HTML pages are loaded dynamically.

    Let’s say, there is a page called Current Page where I’m currently filling some data and is loaded like:

    Login page > Main Page > Form Page > Data Page > Current Page

    The data in Current Page are fetched via web-service calls, from SQLite that are processed or pulled in previous pages (Main Page, Form Page, Data Page).

    From the Current Page, I opened my second app and did some operations in second app.

    When I open my first app from the second app, then it starts from the beginning i.e. from Login Page. I want to restore the state of first app (i.e. Current Page) without losing any data.

    I can’t directly navigate to the ‘Current Page’ using window.location.href = "CurrentPage.html" since it’s data are associated with the previous pages (Main Page, Form Page, Data Page)

    How to maintain state in this case or how to navigate to the ‘Current Page’ without losing any data when I open app from second app.

    Hope you understand my problem. Please provide some suggestions on this.

    Thank you!

    Kishor Bikram Oli

    Wednesday, July 27, 2016 5:09 PM

All replies

  • Hi Geekishor,

    You can save the user's data into the SessionState right before calling the second app.
    Then when your first app is reactivated by the second app, you can check those data in SessionState and restore your first app the way you want.

    This is a common practice as well to handle the app lifecycle where your app can be suspended and terminated by Windows while the user didn't explicitly closed the app.

    See this link for an example : Manage app lifecycle and state (HTML)

    Wednesday, July 27, 2016 7:35 PM
  • Hi Timothe Lariviere, Thank you for your input. I tried this sample and seems to work.

    But in my case, data are in enormous number and also current page is loaded dynamically only after many operations like Login, web-service calls, pull data from SQLite and many more.

    I think this will be very complex saving all the data in SessionState and navigating directly to current page skipping all the operations like Login, SQLite data pull, web-service calls.

    How about app-to-app communication that is newly available feature in Windows 10. Will this help me to solve my problem. Please let me know your thoughts. Thank you!

    Kishor Bikram Oli

    Friday, July 29, 2016 9:30 AM
  • Well, if you're not constrainted by keeping your 2 apps Windows 8.1 only, you can try to migrate them to UWP and have access to those new APIs for communication.

    So if I understand correctly, you need from the 1st app to open the 2nd app so that the user can use it and upon some kind of validation, it will return the result to the 1st app ?

    So as you're saying that you have a lot of data to share between the apps, the "Launching an app to get a result back" part of the article won't work.
    Because the first app would have to launch the second app through a URI, which can't hold that much data especially if it's complex objects.

    What you can try is the "Sharing a file between apps".
    That way you can save your data let's say in a JSON format, so that the 2nd app can read it.
    The reply would be either by writing in the same/another file or by the ReportCompleted method, depending on your need.

    Friday, July 29, 2016 10:29 AM