简体   繁体   中英

PhoneGap on Android - localStorage not working

I have a mobile app based on PhoneGap 2.0, that uses LocalStorage. The localstorage on my device with Android 2.3.3 seems to work like sessionStorage ie the information is persisted and can be used only until the app is closed. When I restart the app that information is gone.

I access/store information like that:

function onDeviceReady() 
{
    if(typeof(Storage)!=="undefined")
    {
        if(localStorage.getItem("test") != null)
        {
            alert(localStorage.getItem("test"));
        }
        ...
        localStorage.setItem("test", "test Value");
    }
}

The same solution works fine when deployed to a WP7 phone (using window.localStorage). I tried with and without "window." prefix. I also tried assigning values from localStorage to global variables before displaying, as suggested in some threads, with no luck.

This is working for me on the Phonegap 2.1 release candidate, on Nexus 7 running 4.1.1, and the emulator running 2.3.3. What device are you testing on?

It's possible this is a bug in 2.0 that's been fixed, though I don't see anything like this in the bug tracker. Try it with 2.1.0 and see what happens?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM