Home > other >  App crashes using the new Android 12 Splash Screen API
App crashes using the new Android 12 Splash Screen API

Time:01-10

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.

CodePudding user response:

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. (Check the Manifest file of my app for example: https://github.com/Sujal1245/WALLisWALL-Wallpaper-App/blob/master/app/src/main/AndroidManifest.xml)

  •  Tags:  
  • Related