25

I'm facing a huge problem developing an Android app which use a Webview to display datas. The website i'm using in the webview use localStorage API of HTML 5.

To enable this feature i've set the webview setting like this :

webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);

So the localStorage API works but when I close the app (and kill the process), localStorage is completly erased and when I reload it, all my datas are lost.

My question is simple : How to make DomStorage of a Webview persistant even when we close the app ?

Thank you for all you future answers.

Gabe
  • 84,912
  • 12
  • 139
  • 238
jimroot25
  • 251
  • 1
  • 3
  • 3

6 Answers6

10

Did you set the DatabasePath? Android doesn't know where to save the DOMDatabase by default, if you don't set it calling

webview.getSettings().setDatabasePath()
Panthro
  • 3,247
  • 2
  • 32
  • 37
  • 1
    note you can only call setDatabase path once - subsequent calls are ignored. You need to terminate the application to force WebKit to reload (calling Activity.finish() will not guarantee this) before any subsequent calls take affect – gheese Sep 13 '12 at 07:09
10
// Confimed on android 2.1 emulator
// enable javascript localStorage

WebSettings webSettings = myWebView.getSettings();
webSettings.setDomStorageEnabled(true);   // localStorage

// e.g., if your package is www.myapp.whatever;
webSettings.setDatabasePath("/data/data/www.myapp.whatever/databases/");
quietmint
  • 13,885
  • 6
  • 48
  • 73
ozmike
  • 2,738
  • 1
  • 33
  • 40
  • what about in andorid 2.2 , whether its working or not, because for me in 2.1 its working but not in 2.2 – Karthi Oct 12 '11 at 06:37
  • If you are having "setDatabasePath deprecated" error with this usage, in chromium webkit browser handles the path itself. [See this answer for more](http://stackoverflow.com/questions/21319279/android-webview-setdatabasepath-deprecated) – Burak Tokak Jan 06 '16 at 00:01
7

You must enable the database as well as setting its path:

webview.getSettings().setDatabaseEnabled(true);
webview.getSettings().setDatabasePath();
webSettings.setDomStorageEnabled(true);

The first line caught me out for quite a while...

rdougan
  • 7,217
  • 2
  • 34
  • 63
0

The above post is mixing two diferent concepts:

To make your data persist after the session is closed make sure you are not using (on your javascript) sessionStorage

DOMStorage and database storage are two different things. Database storage is not part of HTML5: http://www.tutorialspoint.com/html5/html5_web_sql.htm

DOMStorage is more related to HTML5 and includes session storage, which -by design- will disappear when you close your browser. You will find more hits via searching "local storage' that 'DOM storage'.

http://viralpatel.net/blogs/introduction-html5-domstorage-api-example/

Thus, to enable DOMstorage you just need this: webSettings.setDomStorageEnabled(true);

nurieta
  • 1,615
  • 15
  • 6
0

This issue was answered in this post with an update for Android v4.1.1.

Community
  • 1
  • 1
Lisarien
  • 1,136
  • 1
  • 12
  • 24
0
WebSettings webSettings = myWebView.getSettings();
webView.getSettings().setDatabaseEnabled(true);

That is all you need for the latest APIs. The database path is managed automatically by chrome

vinit
  • 155
  • 14