20

I have a native Android 2.1 application that hosts a web view. I load up a site that contains javascript that uses the LocalStorage feature. When the application is running localStorage works fine. When some users exit the application and restart it, all the values are gone. I'm not seeing this problem in my Motrola Droid or Sprint EVO, but there is a report of users in the field with this issue.

Does anyone know what I am missing? I'm setting the following flag to true.

settings.setDomStorageEnabled(true);
Kevin
  • 9,309
  • 12
  • 44
  • 51
  • Do you know what devices are having this problem with your app? I am not aware of a problem with this feature, at least on Android 2.x devices. – CommonsWare Jun 02 '10 at 22:08
  • It looks like a couple Motorola droids are failing. I Have one that came w/ 2.1 from the factory and it seems to be working fine. – Kevin Jun 02 '10 at 22:51
  • http://stackoverflow.com/questions/4157184/android-making-webview-domstorage-persistant-after-app-closed – rdmueller Feb 11 '11 at 08:39
  • I am facing exact the same problem. Anybody know what the path should be set to? (webview.getSettings().setDatabasePath()) – dongshengcn Feb 16 '11 at 17:40
  • 1
    I figured that out. It works after I set it to the default path for all the databases for my app: /data/data/package-name/databases/. I am wondering why android does that for us automatically. :( – dongshengcn Feb 17 '11 at 02:10
  • 1
    @Dongshengcn Maybe you could post a full summary of the solution and accept it as an answer they people coming across the question in future know it's solved? This is probably a pretty common problem (4 question upvotes) so it's nice to know there's a solution. – jelford May 15 '11 at 09:13
  • @Kevin please accept one of the answer if it solved your problem, personally I had the same and dongshengcn's answer did the trick! – Guillaume Aug 22 '13 at 08:01

5 Answers5

18

To make the local storage work for your own WebView in Android, you need to make sure the WebView is using the correct file, and the local storage is enabled like this:

String packageName = "com.dongshengcn.android";
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDatabasePath("/data/data/"+packageName+"/databases");
settings.setDomStorageEnabled(true);

Where "com.dongshengcn.android" should be replaced with your own android app pacakge name.

dongshengcn
  • 6,434
  • 7
  • 35
  • 44
  • 5
    Hardcoding the path isn't a proper solution. Use this.getApplicationContext().getDir("databases", Context.MODE_PRIVATE).getPath(); instead as it would still work if the /data/data/ path changes with some new API version. – MrMaffen Jul 01 '14 at 13:12
11

Just for complete the previous responses which did not allow to resolve the issue in my case.

I'm working with Android 4.1.1. My app is using local storage within a Webview and I was getting the same issue as in the original question: the local storage works fine until I kill the app. In this case, the data was lost.

By inspiring me from previous responses, and especially from @diyism, I was able to solve my issue with this:

String databasePath = this.getApplicationContext().getDir("databases", Context.MODE_PRIVATE).getPath();
settingsMenu.setDatabasePath(databasePath);

In fact, as written in the setDatabasePath() documentation: to function correctly, this method must be called with a path to which the application can write.

Lisarien
  • 1,136
  • 1
  • 12
  • 24
7

setDatabasePath() method was deprecated in API level 19. I advise you to use storage locale like this:

webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
}
SBotirov
  • 13,872
  • 7
  • 59
  • 81
4

only need these two lines:

this.getSettings().setDomStorageEnabled(true); //enable to use "window.localStorage['my']='hello1'", in webview js on >= android 2.0
this.getSettings().setDatabasePath("/data/data/"+this.context.getPackageName()+"/databases/"); //if no set or wrong path, variables disappear on killed
diyism
  • 12,477
  • 5
  • 46
  • 46
3
// Confimed on android 2.1 emulator

// enable javascript localStorage


WebSettings webSettings = myWebView.getSettings();

webSettings.setDomStorageEnabled(true);   // localStorage

// eg if your package

// package www.myapp.whatever;

// eg webSettings.setDatabasePath("/data/data/www.myapp.whatever/databases/");

webSettings.setDatabasePath("/data/data/packagename/databases/");

this works

ozmike
  • 2,738
  • 1
  • 33
  • 40