Android 12 Splash Api issue

Viewed 204

I am using android 12 splash api. When device is rotated app shows white background below status bar. I think it happens because my splash background is white but I don't want to show it when fragment is loaded. How to resolve this issue?

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

MainActivity.kt:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        installSplashScreen()
        setContentView(R.layout.activity_main)
    }
}

fragment_dashboard:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/logo_blue"
    tools:context=".ui.Dashboard.DashboardFragment">

</androidx.constraintlayout.widget.ConstraintLayout>

DashboardFragment.kt:

@AndroidEntryPoint
class DashboardFragment : Fragment() {

    private var _binding: FragmentDashboardBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        _binding = FragmentDashboardBinding.inflate(inflater, container, false)

        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AAAAccountsKotlin" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/logo_blue</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/logo_blue</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">@color/logo_blue</item>

        <!-- Customize your theme here. -->
    </style>

    <style name="Theme.CustomSplashScreenTheme" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/white</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/splash_icon</item>
        <item name="postSplashScreenTheme">@style/Theme.AAAAccountsKotlin</item>
        <item name="windowSplashScreenAnimationDuration">3000</item>
    </style>

</resources>

Dependency:

implementation 'androidx.core:core-splashscreen:1.0.0'

Fragment Without Splash Api: enter image description here

Fragment with Splash Api: enter image description here

Issue in red: enter image description here

1 Answers

You need to change a setup bit and update your Splash API version to

implementation("androidx.core:core-splashscreen:1.0.0")

For Light mode

Create a a folder for light in splash screen

    <?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Splash Screen Theme. -->
    <style name="Theme.AppSplash" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/dark</item>
        <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher_round</item>
        <item name="windowSplashScreenAnimationDuration">6000</item>
        <item name="postSplashScreenTheme">@style/Theme.SplishSplash</item>

        <!-- Status bar and Nav bar configs -->
        <item name="android:statusBarColor" tools:targetApi="l">@color/dark</item>
        <item name="android:navigationBarColor">@color/dark</item>
        <item name="android:windowLightStatusBar">false</item>
    </style>
</resources>

For Theme.XML

    <resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SplishSplash" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryVariant">@color/primaryDarkColor</item>
        <item name="colorOnPrimary">@color/primaryTextColor</item>

        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/secondaryColor</item>
        <item name="colorSecondaryVariant">@color/secondaryDarkColor</item>
        <item name="colorOnSecondary">@color/secondaryTextColor</item>

        <!-- Status bar & Nav bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">@color/black</item>
        <item name="android:windowLightStatusBar">true</item>

        <!-- Customize your theme here. -->
        <item name="backgroundColor">@color/white</item>
        <item name="android:background">@color/white</item>
        <item name="colorOnBackground">@color/black</item>
        <item name="colorSurface">@color/white</item>
        <item name="colorOnSurface">@color/black</item>
    </style>

</resources>
Related