Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My Android HTML application is losing values stored in localStorage when it shuts down. Anyone else see this issue?

Tags:

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);
like image 843
Kevin Avatar asked Jun 02 '10 20:06

Kevin


2 Answers

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.

like image 182
dongshengcn Avatar answered Sep 18 '22 09:09

dongshengcn


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.

like image 26
Lisarien Avatar answered Sep 21 '22 09:09

Lisarien