Note 11 / Flutter / Ads / Mobile
Why returning from a browser or the share sheet shows an app-open ad: marking external hand-offs in Flutter
A resume-ad gate that fires when users come back from a web page, a mail app, the share sheet or a purchase. The fix is a mark with its own end point, a source scan that fails on any new exit, and a few traps with browser bottom sheets and SDK-owned flags.
- By
- Trần Thế Anh, Software Engineer
- Published
- Work done
- October and November 2026, Flutter and React Native apps with app-open ads
- Stack
- Flutter, google_mobile_ads, flutter_custom_tabs, WidgetsBindingObserver
- Reading
- 6 min, 1,175 words
A “welcome back” screen or app-open ad is meant for someone who left the app and came back. The code that decides this usually listens for the app lifecycle: the app goes to the background, then resumes, so show the ad. The trouble is that many resumes are not the user coming back. They opened a web page, a mail composer, the share sheet, a permission dialog or a purchase sheet from inside the app, and then returned from that. Showing a full-screen ad there feels like a bug, and the ad placement policy these apps follow treats it as one.
This note describes the structure that fixed this in several apps, and the flaws that earlier fixes had. The same lesson came back several times, each time through a slightly different hole.
The shape of the problem
The gate sees a lifecycle event: paused, then resumed. It cannot tell why. Any code path that deliberately hands the user to something outside the app has to tell the gate “the next resume is mine, do not show an ad”. That is a mark. Three things go wrong with marks:
- Missing marks. A new place that opens a URL, a mailto link, the share sheet, a permission request or an image picker forgets to set one.
- Marks that live too long. A time window (“ignore resumes for the next ten seconds”) suppresses a real return later, or expires before a slow return.
- Marks that never end. Opening failed, no resume came, the mark stays and eats the next genuine one.
Two marks, each with its own end
The design that held up uses two kinds of mark, and neither is a flat flag.
A launch mark for leaving with no clear end, such as a web page. It stays set through any number of leaves until the first touch while the app is in the foreground. Its end is the user’s own touch, since a bottom sheet in the browser fires no lifecycle event on iOS and cannot be waited for. A subtlety: touches in the first 2.5 seconds after opening do not count, unless a return trip has already happened, because a double tap or a browser that is still starting would otherwise clear the mark early. If opening fails, the mark is removed and a translated toast is shown.
A hand-off mark for calls with a clear end, such as await Share.share(...). It wraps the call in a function (awaitExternalHandOff(() => ...)) and lives exactly as long as that call.
The rule that goes with both: never set a bare mark. A mark must always be created by one of these two functions, so that an end point is part of the code that sets it.
Where web pages open
The app’s own legal and help pages should open in a browser bottom sheet: a Partial Custom Tab on Android and a SFSafariViewController page sheet on iOS (flutter_custom_tabs supports both). The policy here asked for a sheet, not a full-screen custom tab (LaunchMode.inAppBrowserView), a hand-built WebView or a separate browser app. In every case the mark is set before opening.
Ad clicks are exits too
When the user taps an ad, the SDK sends them to a store or browser. That return must not trigger an app-open ad. Every ad type calls the same suppress function in its onAdClicked callback: interstitial, rewarded, app open, banner and native, five places in total. A source test counts them.
“Reached Home” has to be recorded at the time of leaving
Some apps show the welcome back screen only if the user has already seen Home. An early version checked whether the Home widget existed in the widget tree when resuming. A widget that is built is not a widget that was shown, and reading it at resume time is too late, since the route may have changed in between. The corrected condition: Home was shown at least once, recorded in a marker widget when it appears, and captured as a snapshot at the moment of leaving. The decision at return uses that snapshot, not a fresh read.
SDK-owned flags clear on SDK callbacks
A flag like “a fullscreen ad is showing” belongs to the ad SDK’s lifecycle. One bug cleared it in the generic resumed handler. But a resume can arrive while the ad is still on screen, so the flag was cleared while the ad was up and later checks read the wrong state. The rule: a flag the SDK owns ends only by the SDK’s own callbacks (with a timeout as backup), and never in a general lifecycle event. A test forbids writing that flag inside didChangeAppLifecycleState.
A related rule for any flag set at the start of an async flow: clear it on every exit, early return, catch and timeout, by putting the clearing in one abort() function or a finally. A flag that is set in one place and cleared in only the main path will stick the first time something throws.
The scan that catches new exits
Rules about “remember to set the mark” fail. A test that reads the source does not. It scans the app’s lib for every call that leaves the app and fails if one is not covered:
grep -rnE "launchUrl\(|launchUrlString\(|LaunchMode\.|openUrl\(|mailto:|tel:|sms:|Share\.|share_plus|openAppSettings|purchasePackage|requestReview|signIn|pickImage|pickFiles|requestPermission" lib
Every hit should be a browser sheet or sit inside a hand-off. The test version does the same with comments stripped, and it also checks that every launchUrl and OAuth sign-in call has a mark before it and a removal when it fails, that no bare marks exist, and that onAdClicked sets the suppress in five places. A permission request counts: the FCM notification permission, the App Tracking Transparency prompt, the consent form at app start, the system settings page, the image picker and Apple sign-in were all exits that needed to be inside a hand-off.
One more pitfall: a fix that was a no-op
The old suppress function for iOS was only effective on iOS. On Android it did nothing, and the Android return still showed an ad. The bug report classed it Critical. Both marks now run on every platform with no platform branch, because “only on iOS” turned out to be the silent failure.
Recurrence
This lesson came back on 2 November 2026 in another app: a subscription paywall and a customer centre screen from the purchase SDK did not carry a mark, so the Android return showed the ad. So the scan above includes purchasePackage and similar calls, and a new SDK screen that can open another app needs to be added to the list.
What I did and did not verify
I read the project’s lessons (the five records are BL-011, BL-040, BL-041, BL-042 and BL-043 in my log), the ads reference notes and the test names they cite. I did not rebuild the app or run its tests while writing, so the commands above are shown as the project records them, with no new output.