2

I use android splash screen api for my app splash screen. I implemented the dark mode theme for my app and everything is working. But on some devices, like the Samsung devices with Android 13 (API 33) when I switched the dark mode to light from the app and the device dark mode is still enabled, the splash screen shows up with a dark theme while I want to show up with a light theme because I changed my app theme to light. In other words, the splash screen loads the theme based on device settings. The switch works correct on android pixel emulators and show up with correct theme.

Theme switch function:

    fun changeTheme(context: Context, state: DarkModeState) {
        SharedPrefsHelper.putInt(Consts.Prefs.DARK_MODE, state.ordinal)
        val mode = when {
            state == DarkModeState.ON && isSdk31AndUp -> UiModeManager.MODE_NIGHT_YES
            state == DarkModeState.ON -> AppCompatDelegate.MODE_NIGHT_YES
            state == DarkModeState.OFF && isSdk31AndUp -> UiModeManager.MODE_NIGHT_NO
            state == DarkModeState.OFF -> AppCompatDelegate.MODE_NIGHT_NO
            state == DarkModeState.SYSTEM && isSdk31AndUp -> UiModeManager.MODE_NIGHT_AUTO
            state == DarkModeState.SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
            else -> return
        }

        if (isSdk31AndUp) {
            context.applicationContext.getSystemService(UiModeManager::class.java).apply {
                setApplicationNightMode(mode)
            }
            return
        }
        AppCompatDelegate.setDefaultNightMode(mode)
    }

BaseTheme:

    <style name="BaseTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- No Actionbar theme -->
        <item name="colorPrimary">@color/primary</item>
        <item name="colorAccent">@color/primary</item>
        <item name="android:statusBarColor">@color/layerTwoBg</item>
        <item name="android:windowBackground">@color/surfaceBg</item>
        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    </style>

res/values/splash_theme.xml:

    <style name="Theme.AppSplash" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/white</item>
        <item name="android:windowBackground">@color/white</item>
        <item name="postSplashScreenTheme">@style/AppTheme</item>
        <item name="android:statusBarColor">@color/white</item>
        <item name="android:navigationBarColor">@color/white</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    </style>

res/values-night/splash_theme.xml:

    <style name="Theme.AppSplash" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/surfaceBg</item>
        <item name="android:windowBackground">@color/surfaceBg</item>
        <item name="postSplashScreenTheme">@style/AppTheme</item>
        <item name="android:statusBarColor">@color/surfaceBg</item>
        <item name="android:navigationBarColor">@color/surfaceBg</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">false</item>
        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    </style>

Launcher actiivty:

override fun onCreate(savedInstanceState: Bundle?) {
        installSplashScreen()
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
}

Androidmanifest:

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:enableOnBackInvokedCallback="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@drawable/ic_launcher"
        android:screenOrientation="portrait"
        android:supportsRtl="false"
        android:theme="@style/Theme.AppSplash"
        tools:ignore="HardcodedDebugMode"
        tools:replace="android:supportsRtl">

        <activity
            android:name=".view.main.MainActivity"
            android:exported="true"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

build.gradle:

implementation "androidx.core:core-splashscreen:1.0.1"

0 Answers0