Handling Android 12 Splash Screen in existing projects


Google introduced a new Splash Screen behavior in Android 12 and it will automatically generate a splash screen on the launch of any application. This is causing an issue of showing two splash screens for some applications where a splash screen is already implemented. One will be the system generated one and the other will be an app splash screen. 

We don’t have any API to remove the system generated splash screen and it is common behavior for all apps in Android 12. Now the only option to handle this issue is to remove the existing splash screen but it requires so much code refactor because it handles more complex scenarios such as performing app initialization and fetching data before the user is taken into the app.
So we can resolve this issue by Using some workarounds

Approach 1:
Add <item name=”android:windowIsTranslucent”>true</item> to your style

<style name="RemoveAppSplashTheme" parent="@style/AppBaseTheme">
<item name="android:windowIsTranslucent">true</item>
</style>

Apply this theme to your app splash screen Activity.

<activity
   android:name="com.app.SplashScreenActivity"
   android:theme="@style/RemoveAppSplashTheme"
   android:launchMode="singleInstance" />

This theme will replace the app default splash screen with a transparent screen. This will solve the two splash screen problem when the application has an existing splash screen.

If an application takes more time to load then this approach may look like the app is not responding because the system splash screen is invisible.

Approach 2:

We can suspend the application to draw an existing splash screen and show the system splash screen until the application is ready. This will solve the app delay problem

private void setupOnPreDrawListenerToRootView() {
   View pView = findViewById(android.R.id.content);
   pView.getViewTreeObserver().addOnPreDrawListener(
        new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
               // Check if the initialization is done or not.
               if (isAppInitialized) {
                   // The content is ready, then start main activity.
                   pView.getViewTreeObserver().removeOnPreDrawListener(this);
                   startActivity(new Intent(this, MainActivity.class));  
                   return true;
               } else {
                   // The content is not ready, then suspend.
                   return false;
               }
           }
       });
}

Once the app is ready we need to update the isAppInitialized to true, then we can remove the pre-draw listener and launch the MainActivity. Up to that point, it will hold the application to draw the present splash screen and execute all the app initializations.

Leave A Comment

Your email address will not be published. Required fields are marked *