Skip to content

Commit d5e4d9f

Browse files
romtsnclaude
andcommitted
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>
1 parent 0412cb7 commit d5e4d9f

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

sentry-android-fragment/src/main/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacks.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public class SentryFragmentLifecycleCallbacks(
100100
// "view created -> resumed" span for those paths. startTracing is idempotent, so for the
101101
// normal onFragmentCreated -> onFragmentViewCreated path this is a no-op.
102102
if (fragment.isAdded) {
103+
if (scopes.options.isEnableScreenTracking) {
104+
scopes.configureScope { it.screen = getFragmentName(fragment) }
105+
}
103106
startTracing(fragment)
104107
}
105108
}

sentry-android-fragment/src/test/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacksTest.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ class SentryFragmentLifecycleCallbacksTest {
4343
enableAutoFragmentLifecycleTracing: Boolean = false,
4444
tracesSampleRate: Double? = 1.0,
4545
isAdded: Boolean = true,
46+
enableScreenTracking: Boolean = false,
4647
): SentryFragmentLifecycleCallbacks {
4748
whenever(scopes.options)
48-
.thenReturn(SentryOptions().apply { setTracesSampleRate(tracesSampleRate) })
49+
.thenReturn(
50+
SentryOptions().apply {
51+
setTracesSampleRate(tracesSampleRate)
52+
isEnableScreenTracking = enableScreenTracking
53+
},
54+
)
4955
whenever(span.spanContext)
5056
.thenReturn(SpanContext(SentryId.EMPTY_ID, SpanId.EMPTY_ID, "op", null, null))
5157
whenever(transaction.startChild(any<String>(), any<String>())).thenReturn(span)
@@ -271,6 +277,21 @@ class SentryFragmentLifecycleCallbacksTest {
271277
)
272278
}
273279

280+
@Test
281+
fun `When fragment view is created via detach-attach, it should update screen name`() {
282+
val sut =
283+
fixture.getSut(enableAutoFragmentLifecycleTracing = true, enableScreenTracking = true)
284+
285+
sut.onFragmentViewCreated(
286+
fixture.fragmentManager,
287+
fixture.fragment,
288+
view = mock(),
289+
savedInstanceState = null,
290+
)
291+
292+
verify(fixture.scope).screen = "androidx.fragment.app.Fragment"
293+
}
294+
274295
@Test
275296
fun `When fragment view is created after onFragmentCreated, it should not start a second span`() {
276297
// Normal path: onFragmentCreated already started the span; onFragmentViewCreated is a no-op

0 commit comments

Comments
 (0)