How Back works in a WebView shell
- The user presses Back or swipes from the edge.
- If the web view can go back, the shell calls
goBack()— your page sees apopstateevent or navigates to the previous URL. - 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.
Close a modal with Back
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.replaceStatefor 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
| Bug | Cause | Fix |
|---|---|---|
| Back exits from inside a dialog | Dialog didn't push history | pushState on open, close on popstate |
| Back needs two presses | Duplicate entries from pushing on every render | Only push on real navigation |
| Back returns to a splash page | Redirect on launch | Use location.replace for the redirect, or remove it |
| Back reloads the whole page | Full page links in a single-page app | Use 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.