Skip to content

Commit fdf379d

Browse files
committed
Only call appBackground if crash was on foreground
1 parent 36b31e0 commit fdf379d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SharedSessionRepository.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import kotlinx.coroutines.launch
3434

3535
/** Repository to persist session data to be shared between all app processes. */
3636
internal interface SharedSessionRepository {
37+
val isInForeground: Boolean
38+
3739
fun appBackground()
3840

3941
fun appForeground()
@@ -59,6 +61,9 @@ constructor(
5961
/** Local copy of the session data. Can get out of sync, must be double-checked in datastore. */
6062
internal lateinit var localSessionData: SessionData
6163

64+
override var isInForeground = false
65+
private set
66+
6267
/**
6368
* Either notify the subscribers with general multi-process supported session or fallback local
6469
* session
@@ -95,6 +100,7 @@ constructor(
95100
}
96101

97102
override fun appBackground() {
103+
isInForeground = false
98104
if (!::localSessionData.isInitialized) {
99105
Log.d(TAG, "App backgrounded, but local SessionData not initialized")
100106
return
@@ -115,6 +121,7 @@ constructor(
115121
}
116122

117123
override fun appForeground() {
124+
isInForeground = true
118125
if (!::localSessionData.isInitialized) {
119126
Log.d(TAG, "App foregrounded, but local SessionData not initialized")
120127
return

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/api/CrashEventReceiver.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ object CrashEventReceiver {
3838
@JvmStatic
3939
fun notifyCrashOccurred() {
4040
try {
41-
// Treat the crash as if the app went to the background, and update session state.
42-
SharedSessionRepository.instance.appBackground()
41+
// Treat a foreground crash as if the app went to the background, and update session state.
42+
val sharedSessionRepository = SharedSessionRepository.instance
43+
if (sharedSessionRepository.isInForeground) {
44+
sharedSessionRepository.appBackground()
45+
}
4346
} catch (_: Exception) {
4447
// Catch and suppress any exception to avoid crashing during crash handling.
4548
// This can occur if Firebase or the SDK are in an unexpected state (e.g. FirebaseApp deleted)

0 commit comments

Comments
 (0)