Skip to content

fix(browser): only fire browserFinished when the Custom Tab actually terminates#2561

Open
OS-ruimoreiramendes wants to merge 4 commits into
mainfrom
fix/browser-android-launcher
Open

fix(browser): only fire browserFinished when the Custom Tab actually terminates#2561
OS-ruimoreiramendes wants to merge 4 commits into
mainfrom
fix/browser-android-launcher

Conversation

@OS-ruimoreiramendes

@OS-ruimoreiramendes OS-ruimoreiramendes commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Description

Refactor the Android browser plugin to use ActivityResultLauncher for detecting when the Custom Tab has actually terminated, instead of inferring closure from lifecycle events via the previous EventGroup counter.
The EventGroup helper is removed. Browser now exposes setCustomTabLauncher and BrowserControllerActivity registers a launcher via registerForActivityResult, the browserFinished event is only fired when the launched Custom Tab activity ends.

Fixes #2554.

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation
  • Other (CI, chores, etc.)

Rationale / Problems Fixed

When the user minimizes the Chrome Custom Tab to Picture-in-Picture (or backgrounds the app), the previous heuristic counted TAB_HIDDEN + unbindService and inferred the tab had closed firing browserFinished even though the tab was still open. After that, subsequent minimize/maximize/close events wouldn't fire the event again.

ActivityResultLauncher provides a single, deterministic callback only when the Custom Tab activity
truly finishes, so browserFinished now reflects the actual closed state.

Tests or Reproductions

Reproduced with the repo:
https://github.com/Prinzhorn/capacitor-android-browser-close-event

Manually tested on a physical Android device:

  • Open browser → close via back gesture / X button → browserFinished fires once
  • Open browser → minimize to PiP → maximize → close → browserFinished fires once, only on close
  • Open browser → send app to background → return to app → close browser → browserFinished fires once,
    only on close

Platforms Affected

  • Android
  • iOS
  • Web

@OS-ruimoreiramendes OS-ruimoreiramendes changed the title fix(browser): only fire browserFinished when the Custom Tab actually … fix(browser): only fire browserFinished when the Custom Tab actually terminates Jul 3, 2026
@github-actions

Copy link
Copy Markdown

Released dev build of browser with dev version: 8.0.5-dev-2561-20260716T164713.0

@OS-pedrogustavobilro OS-pedrogustavobilro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple comments, nothing major. Haven't tested yet but overall looks good, noice!


private boolean isCustomTabsOpen = false;
private ActivityResultLauncher<Intent> customTabLauncher;
private Browser implementation;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should set implementation in onDestroy just to clear the reference?

Comment on lines 33 to 38
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra("close")) {
finish();
}
}

@OS-pedrogustavobilro OS-pedrogustavobilro Jul 16, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if we should add the notifyBrowserFinished here as well.

I think beforehand the intent wouldn't fire an event back to the plugin in this situation so maybe not relevant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: browserFinished on Android triggers when browser is minimized

2 participants