Software Archive
Read-only legacy content
17061 Discussions

LocalStorage Values lost once app is killed

Nick_F_2
New Contributor III
3,699 Views

I'm using LocalStorage to store some app configuration values which disappear once the app is killed.

I have tried window.localstorage.setItem and local storage.setItem both are accepted. I understood localstorage.setItem was the cordova version and persisted across app restarts.

Interestingly I have an old app compiled as plain HTML5 app on XDK version 1726 and I use window.localstorage.setItem and those values persist through an app restart.

0 Kudos
8 Replies
Nick_F_2
New Contributor III
3,699 Views

Work fine in emulator. I can load another projects run that in the emulator and local storage values persist across restarts of XDK and moving between projects.

0 Kudos
James_H_1
Beginner
3,699 Views

Is this through the App Preview app or an actual build?  What device are you on? The more information, more likely it is someone can help.

The only thing I can think of is that maybe your root is changing between launches.  To check if that's the case, alert the value intel.xdk.webRoot.  I don't think the webRoot is supposed to change very much, but it could be responsible.  Also, there have been quite a few topics about the Intel XDK plugins not working (and thus causing errors), so make sure its not that the data is just never getting stored because of an error.

0 Kudos
K_shin
Beginner
3,699 Views

I think you can skip the window. This works for me without issues:

localStorage.flipswitch0 = "1";

If you need to make the name variable do this:

localStorage[allScenarios.categories[index].shortname+"Purchased"] == 1;

where allScenarios is an object and index is a variable.

Cheers,
K'shin

0 Kudos
Nick_F_2
New Contributor III
3,699 Views

Hi

It is the actual build is not loading the localStorage values. In the emulator it is fine. This is affecting iOS not Android

0 Kudos
Nick_F_2
New Contributor III
3,699 Views

OK

Found the issue, I had an AppMobi.device.hideStatusBar(); under the document ready function and this was causing the failure under IOS but not Android. Which brings b=me to another issue, I cannot get the HideStatus to work in the App, under the emulator it works fine.

0 Kudos
Alper_B_
New Contributor I
3,699 Views

Hello,

I have the same problem.

I use HTML5 + Cordova Blank project plus AngularJS. Without Cordova build the local storage functionality works fine. However, with the Cordova build, when I set a value at localStorage it works, but when I restart the APP, the values within the LocalStorage is lost.

I have tried both in emulator and on device. The result is same. XDK version 1995

I have tried both window.localStorage and just localStorage.

Here are the code snippets I use:

this.getUserToken = function () {

        try
        {
        return JSON.parse(localStorage.getItem("userToken"));
        }
        catch (err)
        {
            alert(err.message);
            return null;
        }
    };

    this.setUserToken = function (token) {
        return localStorage.setItem("userToken", JSON.stringify(token));
    };

Any help would be super appreciated.



 

0 Kudos
Bhavya_A_
Beginner
3,699 Views

Hello,

I have the same problem.

I use HTML5 + Cordova Blank project .  Cordova build, when I set a value at localStorage it works, but when I restart the APP, the values within the LocalStorage is lost.

I have tried both in emulator and on device. The result is same. XDK version 2727

I am using  localStorage.

Any help would be super appreciated.

 

0 Kudos
PaulF_IntelCorp
Employee
3,699 Views

The localStorage API is not a Cordova or XDK feature, it is an HTML5 API. Thus, its behavior is governed by the "webview" on the target device, not the XDK or Cordova. This is similar to saying, its behavior depends on the version of the browser you're running in. As such, it is not very reliable and hard to characterize over a wide variety of devices.

I suspect that the reason some localStorage values disappear when you "kill" the app is because the webview is not getting the opportunity to flush it's cache and store your new values in a place where they can be reread upon startup. When you "kill" an Android app (as in, swipe it from the recently run list of apps) the Android OS does not give the app an "about to die" event. It is killed immediately, so any cleanup that an app (in this case, the webview runtime) might do before being closed does not happen. It's similar to pulling the plug on your desktop computer while an app is running, if that app did not insure that it's local data was stored to disk before the plug was pulled, your data is gone.

I highly recommend you use the SQLite plugin, even if you only need storage for a few items. This will use native storage. Or, use the File plugin to store some information in a local native file. This way, you have control over when any local data is stored to a file.

0 Kudos
Reply