In the Kotlin based Android app, I have a Splash screen which I have developed with the Style attributes as below code:
Drawable File(This one is for Light, Same is for Dark with different gradient color codes):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#43dc91"
android:startColor="#29abe2" />
</shape>
</item>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/My_Image" />
</item>
</layer-list>
Style also has 2 style.xml files - one for dark and one for light:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_back</item>
</style>
And it is called from the Manifest:
<activity
android:name="com.app.ui.splash.SplashActivity"
android:theme="@style/SplashTheme">
Now the issue is that when my app switches to the dark mode from light mode, user will close the app and then open the app again, for 2 seconds, it still shows the light mode splash and after 2 seconds, the dark mode splash will load.
This could happen because when the app is loaded in the splash activity -> Oncreate, I am verifying that if the app has a preference of dark theme then load the dark mode and then it switch my app to dark mode as below:
private fun setAppTheme() {
when {
userHolder.theme != null -> {
if (userHolder.theme == string_(R.string.text_dark_mode))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
else AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
else -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}
}
What to do in such a case? How to load dark mode splash in the first instance itself while loading it from style and drawable?