Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Making Webview DomStorage persistant after app closed

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.

like image 737
jimroot25 Avatar asked Nov 11 '10 17:11

jimroot25


3 Answers

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()
like image 103
Panthro Avatar answered Nov 02 '22 09:11

Panthro


// 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/");
like image 44
ozmike Avatar answered Nov 02 '22 11:11

ozmike


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...

like image 7
rdougan Avatar answered Nov 02 '22 09:11

rdougan