1. Home
  2. Handbook
  3. Back button in WebView apps

// handbook · make it feel like an app

Handling the Android back button in a WebView app

Android users press Back constantly — it's a system gesture, not a button in your page. In a WebView app it usually means "go back in the page history", so your web app's history decides what Back does.

updated · 3 min read · APK Builder team

Short answerIn most WebView app shells, Back goes back one step in the web page's history and closes the app when there's no history left. So push a history entry when you open a screen, modal or menu, and close it in a popstate handler. Use hash routes in single-page apps.

How Back works in a WebView shell

  1. The user presses Back or swipes from the edge.
  2. If the web view can go back, the shell calls goBack() — your page sees a popstate event or navigates to the previous URL.
  3. If there is no history, the app closes (or the shell asks to confirm).

So the rule is simple: anything the user thinks of as "a screen" should have a history entry.

function openSheet() {
  sheet.hidden = false;
  history.pushState({ sheet: true }, '');      // Back will now close it
}
function closeSheet() {
  if (history.state && history.state.sheet) history.back(); // triggers popstate
  else sheet.hidden = true;
}
addEventListener('popstate', () => { sheet.hidden = true; });

Use the same pattern for side menus, image viewers and search overlays.

Single-page apps

  • Use hash routes (#/settings). Every route change adds a history entry, so Back walks back through screens.
  • Use history.replaceState for changes that shouldn't be steps — switching tabs inside one screen, typing in a search box.
  • On the home screen, don't push extra entries, so Back exits as users expect.

Multi-page sites

Plain links between HTML pages build history automatically. Avoid location.replace() unless you mean it, and avoid redirects on launch (for example a splash page that forwards to home.html), or Back will bounce users into the redirect again.

Games and forms

For a game or a long form, losing progress to an accidental Back is painful. Save state to localStorage as the user goes, and push a history entry when a level or form starts so the first Back press can show a "Leave?" panel instead of exiting.

A tiny hash router with good Back behaviour

const screens = { '': home, 'menu': menu, 'item': item };
function route() {
  const [name, id] = location.hash.slice(2).split('/');   // #/item/42
  (screens[name] || home)(id);
}
addEventListener('hashchange', route);
route();
// Navigate:  location.hash = '#/item/42';   (adds a history entry)
// Replace:   history.replaceState(null, '', '#/menu'); route();

Because every screen is a hash, Back always returns to the previous screen, and the very first Back from the home screen closes the app — exactly what Android users expect.

Common Back-button bugs

BugCauseFix
Back exits from inside a dialogDialog didn't push historypushState on open, close on popstate
Back needs two pressesDuplicate entries from pushing on every renderOnly push on real navigation
Back returns to a splash pageRedirect on launchUse location.replace for the redirect, or remove it
Back reloads the whole pageFull page links in a single-page appUse hash routes

Questions

Why does Back close my app instead of my menu?

The menu didn't add a history entry. Call history.pushState when opening it and close it on popstate.

Can I stop Back from closing the app?

You can push a history entry on the home screen to show a confirmation, but use it sparingly — users expect Back to exit.

Does hash routing help with the back button?

Yes. Each hash change creates a history entry, so Back steps through your screens.

Read next

// ready when you are

Build your APK — free.

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