Android WebView localStorage: enable DOM storage
Codemia Knowledge Hub ·
localStorage works in Android WebView only after you enable DOM storage with settings.domStorageEnabled = true. Pages reach localStorage only from script, so enable JavaScript as well. Leaving DOM storage off is the most common reason localStorage appears to be missing.
Enabling JavaScript and DOM storage
localStorage is part of the browser's DOM storage APIs, so the WebView settings must allow it. A minimal Kotlin setup:
With domStorageEnabled left as false, web code that expects localStorage may fail silently or behave as if storage does not exist. Enabling DOM storage without JavaScript is not enough, because no script runs to use it.
Checking that storage works
A small page script confirms it:
In a correctly configured WebView, the stored value survives page reloads for the same origin.
Storage is scoped by origin
localStorage is not one bucket for the whole WebView. It is scoped to the page origin: protocol, host and port. Data written by https://app.example.com is separate from https://api.example.com and from file:// content.
If a hybrid app seems to lose its data, confirm it loads the same origin every time. Debugging one URL while the app actually loads another origin can hide the mismatch.
file:// pages need extra care
Older hybrid apps often loaded HTML from file:// URLs and expected storage to behave like a normal website. Local file origins are not the same as a hosted origin, which makes this awkward. file:// access is also off by default from Android 11 (API 30), where allowFileAccess defaults to false, and allowFileAccessFromFileURLs and allowUniversalAccessFromFileURLs are deprecated.
The recommended fix is WebViewAssetLoader from androidx.webkit. It serves bundled files from a real https origin, https://appassets.androidplatform.net, so storage is scoped to that one stable origin:
When to use native storage instead
WebView localStorage is not a replacement for native persistence such as SharedPreferences, Room or files. It suits web-layer state inside the embedded page. For large app data or business-critical offline state that belongs to the Android app itself, native storage is the more robust choice.
Related reading
- Angualrjs ng-src async image loading from a function call
- Angular2 async unit testing with jasmine
- Angular 2, async testing and setTimeout
- Angular 2 routing with fragments, async data
- Android what does the clipToPadding Attribute do?
- Android When is onCreateOptionsMenu called during Activity lifecycle?
- Angular 2 spring boot server side events
- Angular 6 - Could not find module @angular-devkit/build-angular
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.