'App crashes using the new Android 12 Splash Screen API

I'm trying to use the new Android 12 Splash Screen API but my app keeps crashing when opening the first activity.

I have MainActivity as my launcher activity without any layout file associated to it. When the app launches I keep the splash screen active while I check the current authentication session.

// in MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val splashScreen = installSplashScreen()
    splashScreen.setKeepVisibleCondition { !authSessionIsReady }
    Amplify.Auth.fetchAuthSession(onFetchSuccess, onFetchError)
}

private val onFetchSuccess = fun(session: AuthSession) {
    authSessionIsReady = true
    when (session.isSignedIn) {
        true -> goToHomeActivity(Amplify.Auth.currentUser.username)
        false -> goToLoginOrSignupActivity()
    }
}

private val goToHomeActivity = fun(username: String) {
    Intent(this, HomeActivity::class.java).apply {
        putExtra(EXTRA_USERNAME, username)
    }.also { startActivity(it) }
    finish()
}

this is my manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.myapp">

    <application
        android:name=".AmplifyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApp.Starting">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginOrSignupActivity"
            android:exported="false" />
        <activity
            android:name=".HomeActivity"
            android:exported="false" />
    </application>

</manifest>

and this is the theme file I'm using

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Splash screen theme. -->
    <style name="Theme.MyApp.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/black</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
        <item name="postSplashScreenTheme">@style/Theme.MyApp</item>
    </style>

    <!-- Base application theme. -->
    <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_200</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

As soon as the authentication result comes back the app crashes w/ the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.LoginOrSignupActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Since the error seems to be You need to use a Theme.AppCompat theme (or descendant) with this activity I tried substituting parent="Theme.MaterialComponents.DayNight.NoActionBar" with parent="Theme.AppCompat.DayNight.NoActionBar" in my theme file but that didn't change anything.



Solution 1:[1]

Change the theme in application tag to

@style/Theme.MyApp

And add theme in the activity tag(of your main activity) to

@style/Theme.MyApp.Starting

Let me know if this works for you.For example, one can check the Manifest file of my app

Solution 2:[2]

The suggested solution didn't work for me. For me the culprit were the newest versions of some androidx libraries. I did quite some testing and here is what is working and what isn't for me. Keep in mind that the versions between the stable and current newest, may or may not cause this issue, but I'm not going to test all of them.

Here are the libraries/artifacts that caused issues for me.

androidx.navigation

androidx.navigation:navigation-fragment-ktx:$version
androidx.navigation:navigation-ui-ktx:$version

(Current as time of writing) Stable: 2.4.2 -> works.

(Current) Latest: 2.5.0-beta01 -> causes the issue.

2.5.0-alpha03 is the latest that didn't cause the issue for me.

androidx.fragment

androidx.fragment:fragment-ktx:$version

Stable: 1.4.1 -> works.

Latest: 1.5.0-beta01 -> causes the issue.

1.5.0-alpha03 is the latest that didn't cause the issue for me.

Solution 3:[3]

I faced the same issue and in case is useful for someone: I fixed it setting (in the manifest file) the splashscreen theme just for the activity that will use de splash screen (not for the entire application), for this particular case it will be:

<application
        android:name=".AmplifyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApp">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/Theme.MyApp.Starting">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginOrSignupActivity"
            android:exported="false" />
        <activity
            android:name=".HomeActivity"
            android:exported="false" />
    </application>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhishek Dutt
Solution 2 pollux552
Solution 3 Marcos Narvaez