@@ -89,6 +89,8 @@ public enum AppStartType {
8989 private boolean shouldSendStartMeasurements = true ;
9090 private final AtomicInteger activeActivitiesCounter = new AtomicInteger ();
9191 private final AtomicBoolean firstDrawDone = new AtomicBoolean (false );
92+ private final AtomicBoolean headlessAppStartCheckScheduled = new AtomicBoolean (false );
93+ private final AtomicBoolean headlessAppStartListenerNotified = new AtomicBoolean (false );
9294 private volatile @ Nullable HeadlessAppStartListener headlessAppStartListener ;
9395 private @ Nullable SentryId appStartTraceId ;
9496 private @ Nullable ApplicationStartInfo cachedStartInfo ;
@@ -171,6 +173,12 @@ public void setAppLaunchedInForeground(final boolean appLaunchedInForeground) {
171173
172174 public void setHeadlessAppStartListener (final @ Nullable HeadlessAppStartListener listener ) {
173175 this .headlessAppStartListener = listener ;
176+ if (listener != null
177+ && isCallbackRegistered
178+ && activeActivitiesCounter .get () == 0
179+ && !firstDrawDone .get ()) {
180+ scheduleHeadlessAppStartCheckOnMain ();
181+ }
174182 }
175183
176184 /** Trace ID from a headless app start transaction, to be reused by a later activity. */
@@ -295,6 +303,8 @@ public void clear() {
295303 firstDrawDone .set (false );
296304 activeActivitiesCounter .set (0 );
297305 firstIdle = -1 ;
306+ headlessAppStartCheckScheduled .set (false );
307+ headlessAppStartListenerNotified .set (false );
298308 headlessAppStartListener = null ;
299309 appStartTraceId = null ;
300310 cachedStartInfo = null ;
@@ -398,16 +408,22 @@ public void registerLifecycleCallbacks(final @NotNull Application application) {
398408 }
399409 }
400410
401- scheduleHeadlessAppStartCheckOnMain ();
411+ if (appStartType == AppStartType .UNKNOWN || headlessAppStartListener != null ) {
412+ scheduleHeadlessAppStartCheckOnMain ();
413+ }
402414 }
403415
404416 private void scheduleHeadlessAppStartCheckOnMain () {
417+ if (!headlessAppStartCheckScheduled .compareAndSet (false , true )) {
418+ return ;
419+ }
405420 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
406421 Looper .getMainLooper ()
407422 .getQueue ()
408423 .addIdleHandler (
409424 () -> {
410425 firstIdle = SystemClock .uptimeMillis ();
426+ headlessAppStartCheckScheduled .set (false );
411427 handleHeadlessAppStartIfNeededOnMain ();
412428 return false ;
413429 });
@@ -416,7 +432,11 @@ private void scheduleHeadlessAppStartCheckOnMain() {
416432 handler .post (
417433 () -> {
418434 firstIdle = SystemClock .uptimeMillis ();
419- handler .post (() -> handleHeadlessAppStartIfNeededOnMain ());
435+ handler .post (
436+ () -> {
437+ headlessAppStartCheckScheduled .set (false );
438+ handleHeadlessAppStartIfNeededOnMain ();
439+ });
420440 });
421441 }
422442 }
@@ -446,7 +466,7 @@ private void handleHeadlessAppStartIfNeededOnMain() {
446466 }
447467
448468 final @ Nullable HeadlessAppStartListener listener = headlessAppStartListener ;
449- if (listener != null ) {
469+ if (listener != null && headlessAppStartListenerNotified . compareAndSet ( false , true ) ) {
450470 resolveHeadlessAppStartEndTime ();
451471 listener .onHeadlessAppStart ();
452472 }
0 commit comments