Skip to content

fix(android-fragment): support detach/attach navigation in fragment tracing#5660

Draft
romtsn wants to merge 2 commits into
mainfrom
fix/fragment-detach-attach-tracing
Draft

fix(android-fragment): support detach/attach navigation in fragment tracing#5660
romtsn wants to merge 2 commits into
mainfrom
fix/fragment-detach-attach-tracing

Conversation

@romtsn

@romtsn romtsn commented Jun 29, 2026

Copy link
Copy Markdown
Member

Summary

Fixes a silent data quality issue where detach/attach tab navigation (and similar patterns) left ui.load spans open until the 30-second activity transaction deadline, producing badly inflated performance data.

Root cause

SentryFragmentLifecycleCallbacks started spans in onFragmentCreated and stopped them in onFragmentStarted (a concession to ViewPager2 which locks background fragments to STARTED). For detach/attach navigation — manual tab switching (transaction.detach(current).attach(next).commit()), ViewPager v1 with FragmentPagerAdapter, and custom navigation frameworks that preserve fragment state — onFragmentCreated is never called for off-screen fragments. Spans would hang silently until the deadline.

Fix

Three targeted changes, all leveraging the fact that startTracing / stopTracing are already idempotent:

Hook Change Why
onFragmentViewCreated +startTracing Enables narrower viewCreated → resumed span for detach/attach paths. No-op if onFragmentCreated already started a span.
onFragmentResumed +stopTracing Ends the span on the detach/attach path where onFragmentStarted may not fire. No-op for the normal path where onFragmentStarted already closed the span.
onFragmentViewDestroyed +stopTracing Failsafe: closes any span for a fragment whose view is destroyed before reaching STARTED or RESUMED.

The normal lifecycle path (onFragmentCreated → onFragmentViewCreated → onFragmentStarted → onFragmentResumed) is completely unaffected — still produces exactly one ui.load span, closed at onFragmentStarted.

What's new for customers

Apps using detach/attach navigation will now see correct, narrower ui.load spans (viewCreated → resumed) instead of timeout-inflated spans. The Bol.com workaround described in the issue context is no longer needed.

Tests

Five new unit tests covering:

  • Detach/attach path: onFragmentViewCreated starts a span when tracing enabled
  • Normal path: onFragmentViewCreated is a no-op when span already running
  • Detach/attach path: onFragmentResumed stops the span
  • Normal path: onFragmentResumed does not double-finish after onFragmentStarted
  • Failsafe: onFragmentViewDestroyed stops a span that never reached STARTED/RESUMED

Not verified

./gradlew spotlessApply apiDump could not run (no JDK in sandbox) — CI will apply formatting and regenerate .api files if needed.

…racing

For detach/attach tab navigation (manual tab switching, ViewPager v1 with
FragmentPagerAdapter, custom navigation frameworks), onFragmentCreated is
skipped for off-screen fragments that are re-attached. Previously this left
ui.load spans open until the 30s activity transaction deadline, producing
inflated performance data.

Fix by calling startTracing in onFragmentViewCreated as well as
onFragmentCreated. startTracing is idempotent (no-op if a span is already
running), so the normal onFragmentCreated -> onFragmentViewCreated path is
unaffected. Add matching stopTracing calls in onFragmentResumed (covers the
detach/attach path where onFragmentStarted may be skipped) and
onFragmentViewDestroyed (failsafe for fragments destroyed before reaching
STARTED or RESUMED). stopTracing is also idempotent, so the normal path is
unaffected.

Co-Authored-By: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor
Fails
🚫 Please consider adding a changelog entry for the next release.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

### Fixes

- support detach/attach navigation in fragment tracing ([#5660](https://github.com/getsentry/sentry-java/pull/5660))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description or adding a skip-changelog label.

Generated by 🚫 dangerJS against 55787df

@sentry

sentry Bot commented Jun 29, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.46.0 (1) release

⚙️ sentry-android Build Distribution Settings

@github-actions

Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 324.00 ms 379.38 ms 55.38 ms
Size 0 B 0 B 0 B

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
4e3e79d 312.02 ms 376.24 ms 64.22 ms
22ff2c7 306.60 ms 336.65 ms 30.05 ms
694d587 305.45 ms 378.38 ms 72.94 ms
b8bd880 314.56 ms 336.50 ms 21.94 ms
a416a65 295.53 ms 373.74 ms 78.21 ms
d15471f 379.40 ms 470.76 ms 91.36 ms
05aa61d 326.06 ms 385.46 ms 59.40 ms
fc5ccaf 279.11 ms 353.34 ms 74.23 ms
6b019b7 319.84 ms 333.15 ms 13.31 ms
6405ec5 310.88 ms 354.56 ms 43.69 ms

App size

Revision Plain With Sentry Diff
4e3e79d 0 B 0 B 0 B
22ff2c7 0 B 0 B 0 B
694d587 1.58 MiB 2.19 MiB 620.06 KiB
b8bd880 1.58 MiB 2.29 MiB 722.92 KiB
a416a65 1.58 MiB 2.12 MiB 555.26 KiB
d15471f 1.58 MiB 2.13 MiB 559.54 KiB
05aa61d 0 B 0 B 0 B
fc5ccaf 1.58 MiB 2.13 MiB 557.54 KiB
6b019b7 0 B 0 B 0 B
6405ec5 1.58 MiB 2.12 MiB 552.23 KiB

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.

2 participants