Skip to content

Commit

Permalink
Fix: Do not start fragment span if not added to the Activity (#1813)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Nov 18, 2021
1 parent 61ea653 commit 6a82066
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
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) {
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

0 comments on commit 6a82066

Please sign in to comment.