1. Home
  2. Handbook
  3. Save data offline

// handbook · prepare your files

Saving data in an offline web app: localStorage vs IndexedDB

An offline app still needs to remember things — scores, settings, a shopping list. The browser storage you already know works inside an APK, with a few app-specific caveats.

updated · 3 min read · APK Builder team

Short answerUse localStorage for small data like settings, scores and short lists (a few megabytes at most). Use IndexedDB for larger or structured data such as hundreds of records or images. Both persist between launches inside the app and are deleted when the user uninstalls or clears the app's storage, so offer an export if the data matters.

Which storage to use

localStorageIndexedDB
Best forSettings, scores, small listsMany records, files, images
APISynchronous key/value stringsAsynchronous database
Practical sizeAround 5 MBMuch larger (a share of free disk)
DifficultyOne lineUse a small wrapper library

A safe localStorage pattern

const KEY = 'myapp:v1';
function load() {
  try { return JSON.parse(localStorage.getItem(KEY)) || {}; }
  catch (e) { return {}; }          // corrupted or blocked storage
}
function save(state) {
  try { localStorage.setItem(KEY, JSON.stringify(state)); }
  catch (e) { alert('Storage is full'); }
}

Prefix keys with your app name and a version (myapp:v1) so you can migrate data later without clashes.

How long the data lasts

  • Survives: closing the app, restarting the phone, installing an update with the same package name and signing key.
  • Deleted: uninstalling, "Clear storage" in app settings, or installing a build with a different package name (it's a different app).

Give users a backup

// Export
const blob = new Blob([JSON.stringify(load())], { type: 'application/json' });
const a = Object.assign(document.createElement('a'), {
  href: URL.createObjectURL(blob), download: 'backup.json' });
a.click();

For import, use <input type="file" accept=".json"> and FileReader. Whether downloads and file pickers work depends on the app shell, so test both after building. Copying the JSON to the clipboard is a reliable fallback.

When you need sync

If users expect their data on several phones, you need a backend (a database service with sign-in). At that point a URL app pointing to your web app is usually easier than a bundled one.

Using IndexedDB without the boilerplate

IndexedDB's raw API is verbose. A small promise wrapper (a few hundred bytes) gives you get, set and del like localStorage, but asynchronous and able to store objects, arrays and binary data. Download the library file into your project rather than loading it from a CDN, so the app keeps working offline.

Changing your data format later

const state = load();
if (!state.version) {            // data saved by version 1 of the app
  state.items = state.list || [];
  delete state.list;
  state.version = 2;
  save(state);
}

Store a version number with your data and upgrade old shapes on launch. Users who skip several app updates still end up with data your new code understands.

What not to store

  • Passwords and API secrets — anything in the app's files or storage can be extracted from the phone.
  • Huge media files — they make backups slow and fill the user's storage; stream them instead.
  • Data you can't afford to lose without an export or a server copy.

The habit tracker, event schedule and ebook reader blueprints all use the safe localStorage pattern shown above.

Questions

Does localStorage work inside an Android APK?

Yes. WebView apps support localStorage and IndexedDB, and the data persists between launches.

Is the data kept when I update the app?

Yes, if the update uses the same package name and signing key.

How much can I store in localStorage?

Plan for about 5 MB. Use IndexedDB for anything larger.

Read next

// ready when you are

Build your APK — free.

No Android Studio, no code, no watermark. Pick your input: