Eman
Eman

Reputation: 1315

Splash API throws You need to use a Theme.AppCompat theme (or descendant) with this activity

I'm trying to follow this android documentation on using their Splash API so my splash screen can work on any version of android I support and I'm hitting a wall. I followed it exactly and I keep getting this when I debug my app:

You need to use a Theme.AppCompat theme (or descendant) with this activity.

// AndroidX Components
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.browser:browser:1.4.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.constraintlayout:constraintlayout-solver:2.0.4'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.core:core-splashscreen:1.0.0-beta02'
implementation "androidx.datastore:datastore-preferences:1.0.0"
implementation 'androidx.fragment:fragment-ktx:1.4.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:2.4.1"
implementation "androidx.navigation:navigation-dynamic-features-fragment:2.4.2"
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
implementation "androidx.navigation:navigation-runtime-ktx:2.4.2"
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
implementation 'androidx.paging:paging-runtime-ktx:3.1.1'
implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.recyclerview:recyclerview-selection:1.1.0'
implementation "androidx.security:security-crypto:1.1.0-alpha03"
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'androidx.webkit:webkit:1.4.0'
implementation "androidx.work:work-runtime-ktx:2.7.1"

// Firebase & Google Play Services
implementation 'com.google.android.gms:play-services-base:18.0.1'
implementation 'com.google.android.play:core:1.10.3'
implementation 'com.google.android.play:core-ktx:1.8.1'
implementation platform('com.google.firebase:firebase-bom:29.0.4')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-crashlytics-ktx'
implementation 'com.google.firebase:firebase-messaging-ktx'
implementation 'com.google.gms:google-services:4.3.10'

// Glide
implementation 'com.github.bumptech.glide:glide:4.13.1'
implementation 'com.github.bumptech.glide:recyclerview-integration:4.13.1'
kapt 'com.github.bumptech.glide:compiler:4.13.1'

// Hilt
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
implementation 'androidx.hilt:hilt-work:1.0.0'
implementation 'com.google.dagger:hilt-android:2.41'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
kapt 'com.google.dagger:hilt-android-compiler:2.41'

// Ktor
implementation 'io.ktor:ktor-client-android:2.0.0'
implementation 'io.ktor:ktor-client-auth:2.0.0'
implementation 'io.ktor:ktor-client-content-negotiation:2.0.0'
implementation 'io.ktor:ktor-serialization-kotlinx-json:2.0.0'

// Kotlin & Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1-native-mt'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1-native-mt'
implementation 'org.jetbrains.kotlinx:kotlinx-datetime:0.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2'

// Misc
implementation 'com.auth0.android:jwtdecode:2.0.1'
implementation "com.github.skydoves:androidveil:1.1.2"
implementation 'com.github.yalantis:ucrop:2.2.8'
implementation 'com.google.android.material:material:1.5.0'
implementation 'com.jakewharton.timber:timber:5.0.1'

// Unit Testing
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
<style name="App.Material" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:navigationBarColor">@color/primary</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textColorPrimary">@color/textColor</item>
    <item name="android:textColorSecondary">@color/mutedTextColor</item>
    <item name="actionModeCloseDrawable">@drawable/close</item>
    <item name="actionModeStyle">@style/ActionModeStyle</item>
    <item name="actionMenuTextColor">#ffffff</item>
    <item name="colorAccent">@color/primary</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary</item>
    <item name="colorError">@color/error</item>
    <item name="colorPrimaryVariant">@color/primary</item>
    <item name="colorSecondaryVariant">@color/primary</item>
    <item name="colorOnPrimary">#ffffff</item>
    <item name="colorOnSecondary">#ffffff</item>
    <item name="colorOnBackground">@color/textColor</item>
    <item name="colorOnError">#ffffff</item>
    <item name="shapeAppearanceLargeComponent">@style/ShapeAppearance.LargeComponent</item>
    <item name="shapeAppearanceMediumComponent">@style/ShapeAppearance.MediumComponent</item>
    <item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.SmallComponent</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="App.Material.Splash" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/primaryColor</item>
    <item name="windowSplashScreenAnimatedIcon">@drawable/splash_logo</item>
    <item name="postSplashScreenTheme">@style/App.Material</item>

    <!-- Status bar and Nav bar configs -->
    <item name="android:statusBarColor" tools:targetApi="l">@color/primaryColor</item>
    <item name="android:navigationBarColor">@color/primaryColor</item>
</style>
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)
{
    val splashScreen = installSplashScreen()

    super.onCreate(savedInstanceState, persistentState)

    // Keep the splash screen visible for this Activity
    splashScreen.setKeepOnScreenCondition { true }

    // ... rest of my app's code
}

I believe I followed how it's meant to be setup, so I'm just lost at this point on why it's not working at all and throwing a fit.

Upvotes: 4

Views: 1991

Answers (4)

Robin
Robin

Reputation: 1345

For my case installSplashScreen() wasn't called on the activity before setContentView so there was a crash

override fun onCreate(savedInstanceState: Bundle?) {

    val splashScreen = installSplashScreen()
    
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)

Upvotes: 0

Hardik Kubavat
Hardik Kubavat

Reputation: 301

Add this line in SplashScreen Theme like this

<item name="postSplashScreenTheme">@style/Theme.YOURAPPTHEME</item>

Upvotes: 4

Kamil Komnacki
Kamil Komnacki

Reputation: 456

I've faced the same error while migrating to the new SplashScreen API. Here is the answer that helps me to solve this: https://stackoverflow.com/a/72711707/5985886

Upvotes: -2

Eman
Eman

Reputation: 1315

After a lot of trialing, I discovered my issue and I've reported the issue on their bug tracker. Apparently if you have persistentState setup on onCreate() it triggers this error but removing it, the app will not crash.

If someone runs into that, hopefully this helps you and hopefully the fine people at Google will address this or make a note that you can't use that parameter with the splash API.

Upvotes: 5

Related Questions