Fix ReportDrawn* crashing when navigating away from a screen

Doesn't crash when removeReporter() is called multiple times, which is caused by onDismiss and observeReporter().
Relnote: Fix ReportDrawn crashing when navigating away from a screen before report is called.
Fixes: 260506820
Test: testDisposingBeforeReporting
(cherry picked from https://android-review.googlesource.com/q/commit:5713d41811d5a99c93b5d585cbac46bfaa9ed5a6)
Merged-In: Ic46f17d4dfd9a0758398564dce72fbb5f637626e
Change-Id: Ic46f17d4dfd9a0758398564dce72fbb5f637626e
diff --git a/activity/activity-compose/src/androidTest/java/androidx/activity/compose/ReportDrawnTest.kt b/activity/activity-compose/src/androidTest/java/androidx/activity/compose/ReportDrawnTest.kt
index 75be5f1..db1fee8 100644
--- a/activity/activity-compose/src/androidTest/java/androidx/activity/compose/ReportDrawnTest.kt
+++ b/activity/activity-compose/src/androidTest/java/androidx/activity/compose/ReportDrawnTest.kt
@@ -265,4 +265,15 @@
         }
         assertThat(localValue).isSameInstanceAs(fullyDrawnReporterOwner)
     }
+
+    @Test
+    fun testDisposingBeforeReporting() {
+        rule.setContent {
+            // Reporting never finishes
+            ReportDrawnWhen { false }
+            // Report that finishes immediatelly
+            ReportDrawn()
+        }
+        // By going out of the scope, both reporters call onDismiss
+    }
 }
diff --git a/activity/activity/src/main/java/androidx/activity/FullyDrawnReporter.kt b/activity/activity/src/main/java/androidx/activity/FullyDrawnReporter.kt
index 8c57628..dd87c18 100644
--- a/activity/activity/src/main/java/androidx/activity/FullyDrawnReporter.kt
+++ b/activity/activity/src/main/java/androidx/activity/FullyDrawnReporter.kt
@@ -103,10 +103,7 @@
      */
     fun removeReporter() {
         synchronized(lock) {
-            if (!reportedFullyDrawn) {
-                check(reporterCount > 0) {
-                    "removeReporter() called when all reporters have already been removed."
-                }
+            if (!reportedFullyDrawn && reporterCount > 0) {
                 reporterCount--
                 postWhenReportersAreDone()
             }
@@ -189,4 +186,4 @@
     } finally {
         removeReporter()
     }
-}
\ No newline at end of file
+}