Skip to content
webtoapkfree

Why is localStorage not saved in my Android WebView app?

WebView localStorage is not saved when DOM storage is off, the page runs from file://, its origin changes, or the 5 MiB quota fills. How to fix each.

GuidesNookesh Karri

Light cover card with the title 'Why is localStorage not saved in my Android WebView app?' in dark text and a green accent bar

WebView localStorage is not saved when the app never turns on DOM storage, when the page runs from a file:// URL, when the page’s origin changes between launches, or when the 5 MiB quota fills up. Each cause has a different fix, and the first one, calling setDomStorageEnabled(true), solves most cases.

A lot of advice online is old. You’ll still see answers telling you to call setDatabasePath(), which hasn’t done anything for years.

WebView localStorage not saved: which cause is yours?

The symptom usually tells you which of the five causes you have. Find your row, then read that section.

What happens Most likely cause Fix
localStorage is null, or the site breaks on load DOM storage is off setDomStorageEnabled(true)
Saved fine, gone after the app restarts, local HTML file file:// page has no stable origin Serve files through WebViewAssetLoader
Saved on one page, missing on another page of the same site http and https pages, or a different host Use one scheme and one host everywhere
QuotaExceededError in the console 5 MiB per-origin limit reached Move large data to IndexedDB
Everything gone after a logout or settings screen App code clears web storage Check for WebStorage.deleteAllData()

To see the actual error, open the page in Chrome DevTools through chrome://inspect. The guide to debugging a WebView with Chrome DevTools walks through it.

DOM storage is off by default

DOM storage, which is what localStorage and sessionStorage run on, is disabled in every new WebView. The WebSettings reference lists the default for setDomStorageEnabled as false, and JavaScript is off by default too.

webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.loadUrl("https://example.com/")

Set both before you call loadUrl. A real example: a to-do web app shows its list in Chrome, but inside the app the page throws Cannot read properties of null (reading 'getItem') and renders nothing. That’s the same failure behind many WebView white screens.

Old fixes that no longer work

setDatabasePath() is the most repeated fix for WebView localStorage not saved, and it does nothing on modern Android. The reference says it was deprecated in API level 19, and that WebView manages database paths itself, so calling it has no effect.

setDatabaseEnabled() is a different thing. It switches on WebSQL, not localStorage. It’s deprecated in API level 35, and Android says it will become a no-op once Chromium drops WebSQL. If your site still uses WebSQL, plan a move to IndexedDB.

Local HTML files and the file:// trap

Pages loaded from file:// have no normal origin, so localStorage can’t reliably tie data to them. MDN’s localStorage page says behaviour for file: URLs is undefined, and that current browsers seem to give each file: URL its own storage. It can also throw a SecurityError.

So a quiz app bundled as file:///android_asset/index.html might save a score on one page and never find it on the next. The local content guide calls file:// and data: URLs opaque origins and points to WebViewAssetLoader instead. It serves the same files from an HTTPS address such as https://appassets.androidplatform.net/assets/index.html, which gives the page a stable origin.

The same logic applies to loadDataWithBaseURL. Android strongly recommends an HTTP(S) base URL, and a stable one keeps storage in the same place across launches. File access also defaults to false for apps targeting Android R and above.

If you build your app with webtoapkfree.com, self-contained HTML sections work offline. Which origin those sections run under isn’t documented, so test that saved data survives a full app restart before you rely on it.

Same site, different origin

localStorage belongs to one origin: one scheme, one host and one port. MDN notes that http://example.com and https://example.com get separate storage, so data saved on one is invisible to the other.

This bites WebView apps that start on http:// and redirect, or that mix www.example.com with example.com. Pick one exact address and use it in the app’s start URL and every internal link. If you still load anything over plain http, the cleartext guide explains why that breaks in other ways too.

The 5 MiB quota

WebView localStorage has a size limit per origin, and writes fail once you pass it. MDN’s storage quotas page says browsers can store up to 5 MiB of local storage per origin, then throw a QuotaExceededError.

Caching images or long JSON responses in localStorage hits this fast. Keep it for small settings and tokens, and put bigger data in IndexedDB.

When the app itself wipes storage

Sometimes the app deletes the data on purpose. The WebStorage class manages Web SQL and HTML5 Web Storage, and deleteAllData() clears all of it.

Search your code for that call, often placed in a logout or “clear cache” handler. If logout should only drop the session, clear the specific keys from JavaScript with localStorage.removeItem() instead.

My take on what to store there

I treat localStorage in a WebView app as a convenience, never a database. Keep login state on the server with cookies, which the cookies and staying logged in guide covers, and keep only preferences in localStorage.

Even fixed, this storage is only best-effort. MDN says best-effort data lasts only while the origin is under quota, the device has space and the user doesn’t delete it. If losing the data would hurt a user, sync it to a server.

FAQ

Do I still need setDatabasePath for localStorage in WebView?

No. setDatabasePath was deprecated in API level 19, and calling it has no effect because WebView manages database paths itself. Enable DOM storage instead.

Why is localStorage null in my WebView?

DOM storage is off by default in a WebView. Call setDomStorageEnabled(true) on the WebView’s settings before loading the page, and turn on JavaScript too.

Why does localStorage work in Chrome but not with my local HTML file?

Pages loaded from file:// don’t have a normal origin. Browsers may give each file: URL its own storage or throw a SecurityError. Serve the files over an HTTPS URL with WebViewAssetLoader.

How much data can localStorage hold in a WebView?

Browsers can store up to 5 MiB of local storage per origin. Past that, setItem throws a QuotaExceededError, so larger data belongs in IndexedDB.

Sources