Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Do not start fragment span if not added to the Activity #1813

Merged
merged 2 commits into from Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

* Feat: Refactor OkHttp and Apollo to Kotlin functional interfaces (#1797)
* Feat: Add secondary constructor to SentryInstrumentation (#1804)
* Fix: Do not start fragment span if not added to the Activity (#1813)

## 5.4.0

Expand Down
Expand Up @@ -57,7 +57,11 @@ class SentryFragmentLifecycleCallbacks(
) {
addBreadcrumb(fragment, "created")

startTracing(fragment)
// we only start the tracing for the fragment if the fragment has been added to its activity
// and not only to the backstack
if (fragment.isAdded) {
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
startTracing(fragment)
}
}

override fun onFragmentViewCreated(
Expand Down
Expand Up @@ -36,7 +36,8 @@ class SentryFragmentLifecycleCallbacksTest {
fun getSut(
enableFragmentLifecycleBreadcrumbs: Boolean = true,
enableAutoFragmentLifecycleTracing: Boolean = false,
tracesSampleRate: Double? = 1.0
tracesSampleRate: Double? = 1.0,
isAdded: Boolean = true
): SentryFragmentLifecycleCallbacks {
whenever(hub.options).thenReturn(
SentryOptions().apply {
Expand All @@ -48,6 +49,7 @@ class SentryFragmentLifecycleCallbacksTest {
whenever(hub.configureScope(any())).thenAnswer {
(it.arguments[0] as ScopeCallback).run(scope)
}
whenever(fragment.isAdded).thenReturn(isAdded)
return SentryFragmentLifecycleCallbacks(
hub = hub,
enableFragmentLifecycleBreadcrumbs = enableFragmentLifecycleBreadcrumbs,
Expand Down Expand Up @@ -196,6 +198,15 @@ class SentryFragmentLifecycleCallbacksTest {
)
}

@Test
fun `When fragment is created but not added to activity, it should not start tracing`() {
val sut = fixture.getSut(enableAutoFragmentLifecycleTracing = true, isAdded = false)

sut.onFragmentCreated(fixture.fragmentManager, fixture.fragment, savedInstanceState = null)

verify(fixture.transaction, never()).startChild(any(), any())
}

@Test
fun `When tracing is already running, do not start again`() {
val sut = fixture.getSut(enableAutoFragmentLifecycleTracing = true)
Expand Down