Skip to content

Commit df60eec

Browse files
romtsncodex
andcommitted
fix(replay): Ignore lifecycle callbacks before registration
Drop foreground and background callbacks received before Replay is registered instead of leaving stale work on the main queue. Clarify the manual replay API documentation. Refs JAVA-325 Co-Authored-By: Codex <noreply@openai.com>
1 parent 0582fec commit df60eec

3 files changed

Lines changed: 19 additions & 11 deletions

File tree

‎sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayIntegration.kt‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ public class ReplayIntegration(
167167
}
168168

169169
override fun onAppForegrounded(startNewSession: Boolean) {
170+
if (!isEnabled.get()) {
171+
return
172+
}
170173
enqueueOnMainThread {
171-
if (!isEnabled.get()) {
172-
return@enqueueOnMainThread
173-
}
174174
if (startNewSession) {
175175
val isFullSession = sample(options.sessionReplay.sessionSampleRate)
176176
if (!isFullSession && !options.sessionReplay.isSessionReplayForErrorsEnabled) {
@@ -187,6 +187,9 @@ public class ReplayIntegration(
187187
}
188188

189189
override fun onAppBackgrounded() {
190+
if (!isEnabled.get()) {
191+
return
192+
}
190193
enqueueOnMainThread { pauseInternal() }
191194
}
192195

‎sentry-android-replay/src/test/java/io/sentry/android/replay/ReplayIntegrationTest.kt‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,14 @@ class ReplayIntegrationTest {
226226
}
227227

228228
@Test
229-
fun `foreground before register does nothing`() {
230-
val replay = fixture.getSut(context)
229+
fun `lifecycle callbacks before register are not enqueued`() {
230+
val mainLooperHandler = mock<MainLooperHandler>()
231+
val replay = fixture.getSut(context, mainLooperHandler = mainLooperHandler)
231232

232233
replay.onAppForegrounded(true)
234+
replay.onAppBackgrounded()
233235

234-
assertThat(replay.isRecording).isFalse()
236+
verify(mainLooperHandler, never()).post(any())
235237
}
236238

237239
@Test

‎sentry/src/main/java/io/sentry/IReplayApi.java‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ public interface IReplayApi {
1111

1212
/**
1313
* Starts replay buffering. The rolling buffer is sent when {@link #flush()} is called or an error
14-
* is captured. After the buffer is sent, recording continues in session mode unless the process
15-
* is terminating.
14+
* is captured and selected by {@link SentryReplayOptions#getOnErrorSampleRate()}. After the
15+
* buffer is sent, recording continues in session mode unless the process is terminating.
1616
*/
1717
void startBuffering();
1818

19-
/** Stops the current replay. A subsequent {@link #start()} begins a new replay session. */
19+
/**
20+
* Stops the current replay in either session or buffer mode. A subsequent {@link #start()} begins
21+
* a new replay session.
22+
*/
2023
void stop();
2124

2225
/**
23-
* Pauses the current replay until {@link #resume()} is called. This can be used to avoid
24-
* recording sensitive screens, such as PIN entry.
26+
* Pauses the current replay in either session or buffer mode until {@link #resume()} is called.
27+
* This can be used to avoid recording sensitive screens, such as PIN entry.
2528
*/
2629
void pause();
2730

0 commit comments

Comments
 (0)