From 70d4924884685f313c2bba699abd428073f53c0d Mon Sep 17 00:00:00 2001 From: "pouria.amini" Date: Thu, 22 Aug 2024 09:18:15 -0700 Subject: [PATCH] fix: add debug statements when a fragment lifecycle callback is not registered --- .../android/internal/fragments/FragmentActivityHandler.kt | 6 +++--- .../com/amplitude/android/utilities/DefaultEventUtils.kt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/amplitude/android/internal/fragments/FragmentActivityHandler.kt b/android/src/main/java/com/amplitude/android/internal/fragments/FragmentActivityHandler.kt index f9aabeab..9e73a7a6 100644 --- a/android/src/main/java/com/amplitude/android/internal/fragments/FragmentActivityHandler.kt +++ b/android/src/main/java/com/amplitude/android/internal/fragments/FragmentActivityHandler.kt @@ -19,16 +19,16 @@ internal object FragmentActivityHandler { val callback = AutocaptureFragmentLifecycleCallbacks(track, logger) supportFragmentManager.registerFragmentLifecycleCallbacks(callback, false) callbacksMap.getOrPut(this) { mutableListOf() }.add(callback) - } + } ?: logger.debug("Activity is not a FragmentActivity") } - fun Activity.unregisterFragmentLifecycleCallbacks() { + fun Activity.unregisterFragmentLifecycleCallbacks(logger: Logger) { (this as? FragmentActivity)?.apply { callbacksMap.remove(this)?.let { callbacks -> for (callback in callbacks) { supportFragmentManager.unregisterFragmentLifecycleCallbacks(callback) } } - } + } ?: logger.debug("Activity is not a FragmentActivity") } } diff --git a/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt b/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt index ea8ec163..4652dc2b 100644 --- a/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt +++ b/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt @@ -174,7 +174,7 @@ class DefaultEventUtils(private val amplitude: Amplitude) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && LoadClass.isClassAvailable(FRAGMENT_ACTIVITY_CLASS_NAME, amplitude.logger) ) { - activity.unregisterFragmentLifecycleCallbacks() + activity.unregisterFragmentLifecycleCallbacks(amplitude.logger) } }