Search code examples
androidsplash-screenandroid-12

Android 12 splash screen before api 21


I'm trying to upgrade my application to target android 31 which introduces splash screen API so I followed the migration process mentioned here, but after migration the application doesn't run because splash screen API only supports android version 21 and above so what is the process to support older versions than 21?

enter image description here


Solution

  • Androidx SplashScreen class works as following:

    On API 31+ (Android 12+) this class calls the platform methods.

    Prior API 31, the platform behavior is replicated with the exception of the Animated Vector Drawable support on the launch screen.

    the problem is Animated Vector Drawable class was introduced with Android 21 so the current Androidx SplashScreen class backward compatible with Android +21 so i figured out some solution. I created two different splash screen activities one to handle the old versions and the second which use Androidx SplashScreen API. and I made the system lunches splash screen based on the current android version, so I did the following

    1- to allow application compiling and build, I had to add this line to my manifest file

    <uses-sdk tools:overrideLibrary="androidx.core.splashscreen"/>
    

    2- create the new Splash screen activity which uses Androidx SplashScreen API by following the migration steps from documentation.

    3- create bools.xml under values folder

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="new_splash_enabled">false</bool>
        <bool name="old_splash_enabled">false</bool>
    </resources>
    

    4- override bools.xml in values-vX (X is the minSdkVersion) in my case was 16 so i created values-v16 folder in the same level of values folder and create bools.xml under it like below

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="new_splash_enabled">false</bool>
        <bool name="old_splash_enabled">true</bool>
    </resources>
    

    5- override bools.xml in values-vX (X is the min version you want to apply the new SplashScreen so it is any number between 21 and 31)

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="new_splash_enabled">true</bool>
        <bool name="old_splash_enabled">false</bool>
    </resources>
    

    6- in your manifest, i made the system decides which splash activity to launch based on values in bools.xml file

            <activity
                android:name=".NewSplash"
                android:theme="@style/Theme.App.Starting"
                android:enabled="@bool/new_splash_enabled"
                >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity
                android:name=".OldSplash"
                android:enabled="@bool/old_splash_enabled">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>