diff --git a/androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/LauncherActivity.java b/androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/LauncherActivity.java index e50a9b22..3536ba64 100644 --- a/androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/LauncherActivity.java +++ b/androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/LauncherActivity.java @@ -155,6 +155,18 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { return; } + mMetadata = LauncherActivityMetadata.parse(this); + + if (splashScreenNeeded()) { + mSplashScreenStrategy = new PwaWrapperSplashScreenStrategy(this, + mMetadata.splashImageDrawableId, + getColorCompat(mMetadata.splashScreenBackgroundColorId), + getSplashImageScaleType(), + getSplashImageTransformationMatrix(), + mMetadata.splashScreenFadeOutDurationMillis, + mMetadata.fileProviderAuthority); + } + if (shouldLaunchImmediately()) { launchTwa(); } @@ -175,16 +187,12 @@ protected boolean shouldLaunchImmediately() { * {@link #shouldLaunchImmediately()} returns {@code false}. */ protected void launchTwa() { - mMetadata = LauncherActivityMetadata.parse(this); - - if (splashScreenNeeded()) { - mSplashScreenStrategy = new PwaWrapperSplashScreenStrategy(this, - mMetadata.splashImageDrawableId, - getColorCompat(mMetadata.splashScreenBackgroundColorId), - getSplashImageScaleType(), - getSplashImageTransformationMatrix(), - mMetadata.splashScreenFadeOutDurationMillis, - mMetadata.fileProviderAuthority); + // When launching asynchronously, developers should check if the Activity is finishing + // before calling launchTwa(). We double check the condition here and prevent the launch + // if that's the case. + if (isFinishing()) { + Log.d(TAG, "Aborting launchTwa() as Activity is finishing"); + return; } CustomTabColorSchemeParams darkModeColorScheme = new CustomTabColorSchemeParams.Builder() diff --git a/demos/twa-firebase-analytics/src/main/java/com/google/androidbrowserhelper/demos/twa_firebase_analytics/FirebaseAnalyticsLauncherActivity.java b/demos/twa-firebase-analytics/src/main/java/com/google/androidbrowserhelper/demos/twa_firebase_analytics/FirebaseAnalyticsLauncherActivity.java index c2aa68d6..6967939a 100644 --- a/demos/twa-firebase-analytics/src/main/java/com/google/androidbrowserhelper/demos/twa_firebase_analytics/FirebaseAnalyticsLauncherActivity.java +++ b/demos/twa-firebase-analytics/src/main/java/com/google/androidbrowserhelper/demos/twa_firebase_analytics/FirebaseAnalyticsLauncherActivity.java @@ -36,6 +36,11 @@ public class FirebaseAnalyticsLauncherActivity extends LauncherActivity { protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // `super.onCreate()` may have called `finish()`. In this case, we don't do any work. + if (isFinishing()) { + return; + } + FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(this); // Start the asynchronous task to get the Firebase application instance id. diff --git a/demos/twa-offline-first/src/main/java/com/google/androidbrowserhelper/demos/twa_offline_first/OfflineFirstTWALauncherActivity.java b/demos/twa-offline-first/src/main/java/com/google/androidbrowserhelper/demos/twa_offline_first/OfflineFirstTWALauncherActivity.java index 91066d78..50ffd0e8 100644 --- a/demos/twa-offline-first/src/main/java/com/google/androidbrowserhelper/demos/twa_offline_first/OfflineFirstTWALauncherActivity.java +++ b/demos/twa-offline-first/src/main/java/com/google/androidbrowserhelper/demos/twa_offline_first/OfflineFirstTWALauncherActivity.java @@ -30,6 +30,11 @@ public class OfflineFirstTWALauncherActivity extends LauncherActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + // `super.onCreate()` may have called `finish()`. In this case, we don't do any work. + if (isFinishing()) { + return; + } tryLaunchTwa(); }