SIGN IN SIGN UP

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

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

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>

* Format code

* Add changelog entry and detach/attach sample

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Remove duplicate test methods in fragment lifecycle test

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: Update screen name on scope for detach/attach fragment re-attachment

onFragmentCreated is skipped during detach/attach navigation, so the
screen name was never updated for re-attached fragments. Mirror the
screen tracking into onFragmentViewCreated to cover that path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Format code

* Address PR feedback: internalize guards into startTracing and fix sample layout

Move isAdded check, screen tracking, and tracing logic into startTracing()
to deduplicate guards from onFragmentCreated and onFragmentViewCreated.
Fix DetachAttachTabsActivity sample rendering on API 35+ by using
NoActionBar theme and fitsSystemWindows.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: Decouple screen tracking from performance tracing in fragments

Screen name updates on scope should work independently of whether
performance tracing is enabled.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
R
Roman Zavarnitsyn committed
7d8a3947cce374aa65ec6b9e702733ff89a0f29d
Parent: 32ca3d0
Committed by GitHub <noreply@github.com> on 7/2/2026, 11:44:30 AM