I have a html5 application that I wrap with a WebView. To store and retrieve user input values between pages, I use the localStorage html5 feature.
It works fine on my Nexus 4 (Android 4.4.4), but it does not work on Samsung Galaxy Tab 2 (Android 4.3.x) (= nothing happens, but also no error in logcat). Or, to be more clear: on Samsung, it does not work if the html pages are loaded from within the app's asset folder. It does work though if I put the pages on a server, as below in the outcommented line.
However, on Nexus 4, loading from file:///android_asset/ and also if I load the pages on a desktop browser (Chrome, Firefox) from file:// path, it is also working.
Update 1: I just had another user who reported the issue with a LG device, so it does not seem to be Samsung specific.
Update 2: Storing and loading the value from localStorage works fine on all devices on the same page, however, not between different pages. In my example, I can store and retrieve the value on 01_home.html, but when I go to another page in the android_asset folder, I cannot read it anymore (on LG, Samsung devices). Works fine on Nexus 4 though.
Below are the settings of the web view.
webView = (WebView)this.findViewById(R.id.webView);
webViewClient = new MyWebViewClient(this);
webViewClient.setSm(sm);
webView.setWebViewClient(webViewClient);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
webView.loadUrl("file:///android_asset/01_home.html"); // does NOT work!
// webView.loadUrl("http://192.168.178.33/01_home.html"); // does work!
Local storage code in the pages:
// storing
var data = document.getElementById('data').value;
window.localStorage.setItem((1), data);
// reading
document.getElementById('data').value = window.localStorage.getItem(1);
There are answers to this posted elsewhere Android webview & localStorage
Solution proposed:
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/");
}
I found this same bug. Worked on all Nexus devices but all Samsung devices lost my user changes. I know the below method is deprecated but it solved my issue.
webview.getSettings().setDatabasePath("/data/data/" + webview.getContext().getPackageName() + "/databases/");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With