I build a small chore app for families. Kids use the board. Grown-ups have a settings area behind a PIN.
Before shipping an update, I ran a regression pass on the live Android build in an emulator. One step was: open the grown-up area, press Home, open the app again.
The grown-up area was still open. No PIN.
Why it stayed open
The app already listened to AppState. It used the event to sync purchases with the store, and nothing else. The PIN was asked on the way in, and passing it opened a five-minute window. Leaving the app closed neither the grown-up area nor that window.
The obvious fix is to lock on background. That breaks the app's own features, though. The purchase sheet, restore, print, share, the backup file picker and the About links all leave the app for a moment, and a parent who taps Print in settings shouldn't come back to a PIN pad.
The first fix
Every flow the app opens itself now goes through a wrapper that raises a flag while it's open. On background, the app locks unless that flag is up. On return, it locks anyway if it was gone more than five minutes, for the case where someone opens the purchase sheet and hands the phone to a child.
export const AWAY_LOCK_MS = 5 * 60_000;
export function lockOnLeave(expectingExternal: boolean): boolean {
return !expectingExternal;
}
export function lockOnReturn(expectingExternal: boolean, awayMs: number): boolean {
return !expectingExternal || awayMs > AWAY_LOCK_MS;
}
let externalDepth = 0; // counted, so overlapping flows don't clear each other
export async function viaExternal<T>(open: () => Promise<T>): Promise<T> {
externalDepth += 1;
try {
return await open();
} finally {
externalDepth = Math.max(0, externalDepth - 1);
}
}
Only background is watched. On iOS the purchase sheet and Control Center send inactive, so they never lock. Unit tests covered the four cells (flag on or off, inside or past five minutes) and the flag itself. All green.
Print still locked it
A code review of that branch found the hole a few hours later. On Android, expo-print's printAsync resolves as soon as the job is handed to the system print dialog. That happens before the app is paused.
So the order was:
-
printAsyncresolves, and the wrapper lowers the flag. - The print dialog comes up, and the app goes to
background. - The flag is already down, so the app locks.
Links behave the same way, since Linking.openURL returns once the browser is launched.
The fix is a hold. Print and links keep the flag up for three more seconds after they resolve:
export const EARLY_RESOLVE_HOLD_MS = 3000;
await viaExternal(() => Print.printAsync({ html }), { holdMs: EARLY_RESOLVE_HOLD_MS });
The in-app review sheet got wrapped too. On Android it's a separate activity, so it sends background as well.
The next emulator run passed all five checks: back from inside screens, the purchase sheet, back from the grown-up area, Home and reopen (locked), and print and return (not locked).
What's still open
If background ever arrives later than that after a link opens, the app locks on the way back. That's the safe side, so I left it.
The review flagged two more. iOS can take the app switcher snapshot before the lock runs, so a grown-up screen could show in the switcher. A later commit in the same release draws a plain cover over grown-up screens on inactive and background, outside the app's own flows. That commit also dropped the five-minute PIN window, so the PIN is asked on every entry now.
The other one I left: if someone presses Home while a purchase is still pending, the five-minute limit is what eventually locks it.
The runs above were in an emulator.
Takeaway
A lifecycle event says the app left. It doesn't say why. Any grace you add has to carry the reason in from somewhere else, and a promise that resolves early takes that reason with it.
If you've built a lock like this, how did you let your own flows through: a time window after leaving, or a flag set by the code that opened the flow?
This post was drafted with AI help. The numbers and facts come from my own logs and repo.
Top comments (4)
The 3-second hold is the right shape of fix — the real bug is that printAsync resolves when the job is handed off, not when the dialog closes, so the flag and the lifecycle event were racing each other. I hit the same pattern on native Android: onStop fires for system sheets too, so a bare "lock on background" locks you out of your own flows. What held up for me was pairing the flag with a timestamp instead of a fixed hold: record when the external flow launched, and only lock if you're still backgrounded past the window with no pending flow. That way a slow print dialog on a cheap phone can't eat the grace period. One thing I'd watch: the five-minute window means the grown-up area stays open for five minutes after any return — fine for an accidental Home press, but if a kid can reach the print button themselves, the dialog is the weak point, not the lock. Did the emulator run catch the review-sheet case too, or only print and links?
Print, yes. The review sheet and links, no. The review sheet got the same wrapper in code, but none of the five checks in that emulator run opened it, and links weren't in the run either.
The five-minute window is gone now. A later commit in the same release dropped it, so the PIN is asked on every entry. The one place a five-minute limit still matters is a Home press while a purchase is pending.
Recording when the flow launched would handle a slow print dialog better than a fixed three seconds.
We went with the flag, but cleared it on the next return to
activeinstead of when the promise resolves, plus a short timeout for the case where the app never actually leaves (e.g. the sheet fails to open). That removes the guess about how long Android takes to pause afterprintAsyncresolves. For the app switcher on Android, FLAG_SECURE on the grown-up screens also blanks the recents thumbnail, if blocking screenshots there is acceptable for parents.Good approach. I'll try it that way the next time I work on this.