sentry-java icon indicating copy to clipboard operation
sentry-java copied to clipboard

[Starfish] Use a better way to determine app foreground launch

Open markushi opened this issue 1 year ago • 0 comments

Description

Instead of checking for process foreground like we did earlier https://github.com/getsentry/sentry-java/blob/d43311af4de9697012a932ff860c9f2b2ff3d3e0/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java#L136

we could use a similar approach as firebase.

In simplified terms this would looks like the following:

final AtomicBoolean anyActivityCreated = new AtomicBoolean(false);
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacksAdapter() {
  @Override
  public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
    super.onActivityCreated(activity, savedInstanceState);
    anyActivityCreated.set(true);
    unregisterActivityLifecycleCallbacks(this);
  }
});
new Handler(Looper.getMainLooper()).post(() -> {
  final boolean isForegroundAppLaunch = anyActivityCreated.get();
  Log.e(TAG, "onCreate: isForegroundAppLaunch: " + isForegroundAppLaunch);
});

markushi avatar Dec 05 '23 12:12 markushi