Search code examples
androiduser-interfaceandroid-activitysplash-screenandroid-styles

How to make a Splash screen the proper way i.e. not to make it a part of the app process?


I am new to android. I want to add a splash screen to my app. I went through this official documentation.

It says

When a user launches an app while the app's process is not running (a cold start) or the Activity has not been created (a warm start), the following events occur. (The splash screen is never shown during a hot start.) 1) The system shows the splash screen using themes and any animations that you've defined. 2) When the app is ready, the splash screen is dismissed and the app is displayed.

Although implementing an activity as a splash screen like the code snippet given below does the job

class SplashScreenActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_splash_screen)

        supportActionBar?.hide()

        Handler(Looper.getMainLooper()).postDelayed({
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }, 2000)
    }
}

But I guess that's not the conventional way as the splash screen becomes a part of the app process, it starts after a milliseconds long black screen.

I guess this is when the system is fetching up app data resources and it is when a splash screen should be launched at least that's what Google play store or Whatsapp or every other installed app do, the splash screen launch is instant.

Now, as the documentation insists as I try to set the theme attributes I get errors saying Cannot resolve symbol 'android:windowSplashScreenBackground'. This is my style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Theme.Books" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>

        <!-- Unresolved -->
        <item name="android:windowSplashScreenBackground">@color/black</item>
        <item name="android:windowSplashScreenAnimationDuration">1000</item>
        <item name="android:windowSplashScreenAnimatedIcon">@drawable/books_logo</item>
    
    </style>
</resources>

So, 1. how to overcome this?

2. Is there other way around?

3. am I missing something? Is there some ways to somehow detach the activity from app process?

Any kind of help is appreciated. Thanks!


Solution

  • Have you tried following this article?

    Implementing Core Splashscreen API

    What I am getting out of the tutorials:

    You need to call installSplashScreen() before setContentView(R.id.***)

    You need to modify styles.xml as shown in the article, with the custom SplashScreen attributes (and then set the App Theme to that one) see here

    Just remove your SplashScreenActivity, using Handler.postDelayed also isn't a good way of handling this (The Library handles switching shown content itself, just use your MainActivity)