Android - Making Webview DomStorage persistant after app closed

19,905

Solution 1

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()

Solution 2

// 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/");

Solution 3

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

Share:
19,905

Related videos on Youtube

jimroot25
Author by

jimroot25

Updated on May 08, 2022

Comments

  • jimroot25
    jimroot25 about 2 years

    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.

    • jimroot25
      jimroot25 over 13 years
      I've found the solution, you need to set the databasePath in order to save datas : webview.getSettings().setDatabasePath();
    • dongshengcn
      dongshengcn
      Can you share what the path you set it to? I am facing the same problem now.
    • Anthony Webb
      Anthony Webb
      Is it possible to read the local storage vars that are set by the html5 directly from java? I've found the question asked elsewhere, but no one seems to have an answer?
  • Karthi
    Karthi over 12 years
    what about in andorid 2.2 , whether its working or not, because for me in 2.1 its working but not in 2.2
  • gheese
    gheese almost 12 years
    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
  • Burak Tokak
    Burak Tokak over 8 years
    If you are having "setDatabasePath deprecated" error with this usage, in chromium webkit browser handles the path itself. See this answer for more