13

The new API SplashScreen in Android 12 seems good but just like before the sample code in the documentation does not really help explaining the whole and proper implementation. There is also some cases where you might do some task during splash screen in our case this is to launch Firebase Auth so probably the best way is just to opt out on using this new featured API but according to lint warning it seems like it is mandatory and there is no way to opt out.

The application should not provide its own launch screen

Application-defined Launch Screen Starting in Android 12 (API 31+), the application's Launch Screen is provided by the system and the application should not create its own, otherwise the user will see two splashscreen. Please check the SplashScreen class to check how the Splash Screen can be controlled and customized.

How about the backward compatibility for older devices, how to handle it? Is there any codelab project to play and test with?

Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67

4 Answers4

7
  1. Can we opt out of SplashScreen?

It looks like we can't opt out as Android Team is trying to unify the app loading experience: https://9to5google.com/2021/04/21/android-12-dp3-all-apps-now-show-the-same-splash-screen-while-loading-gallery/

  1. How to use it?

If you don't do anything then it will use windowBackground of the theme & your launcher icon & dismissed as soon as your app draws its first frame.

There are bunch of properties that you can modify like background, icon etc: https://developer.android.com/about/versions/12/features/splash-screen#set-theme

  1. What if I want splash to stay longer? Like fetching a local DataBase.

You can use ViewTreeObserver.OnPreDrawListener & make a blocking call from your viewmodel return if it's ready to go ahead.

Activity:

// My Launcher Activity
class MainActivity : AppCompatActivity() {

    private val viewModel : JustDelayViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val content: View = findViewById(android.R.id.content)
        content.viewTreeObserver.addOnPreDrawListener(
            object : ViewTreeObserver.OnPreDrawListener {
                override fun onPreDraw(): Boolean {
                    // Check if the initial data is ready.
                    return if (viewModel.getIsReady()) {
                        // The content is ready; start drawing.
                        content.viewTreeObserver.removeOnPreDrawListener(this)
                        true
                    } else {
                        // The content is not ready; suspend.
                        false
                    }
                }
            }
        )
    }

}

ViewModel:

class JustDelayViewModel : ViewModel() {

    fun getIsReady(): Boolean {
        val result = viewModelScope.runCatching {
            runBlocking {
                //do some blocking call check for Firebase result or something
                delay(5000)
            }
            true //return the result
        }
        return result.isSuccess
    }
}

You can read more about this: https://developer.android.com/about/versions/12/features/splash-screen#suspend-drawing

Vadim Caen
  • 1,526
  • 11
  • 21
Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41
  • Yeah I read those before, but how are we going to handle this together with a support on older devices? It is better to have a codelab or sample project to play with. – Bitwise DEVS May 22 '21 at 09:36
  • 3
    Gee, I think it would have been easier and more flexible if Google just left things well alone in this department and let apps have a little more creative freedom. – Andrew S Jan 25 '22 at 05:34
5

To complement Mayur's answer for older device support.

The new windowSplashScreen* attributes need to be added in the res/values-v31/style.xml file.

Then for the legacy splashscreen it depend of the current implementation of the app.

If the application simply uses a starting theme with a custom windowBackground there is nothing to do since the windowBackground isn't used for the new splash screen (only if it's a simple color).

If the application has some visible splash screen Activity, there will be a double splash screen on Android 12. To solve this, the application can migrate to the windowBackground solution.

If the application really need to keep its splash screen Activity, it can update the layout to match the system splash screen on Android 12 and/or create a smooth transition from the system splash screen to the app splash screen using the SplashScreen.setOnExitAnimationListener()

Vadim Caen
  • 1,526
  • 11
  • 21
  • 1
    This kind of update wasn't received well by many since there are others that prefer not to have splash screen as it is completely unnecessary to other cases. Too bad this is what they did instead of making the platform more stable and having less fragmented behavior. Unifying Android is technically impossible. – Bitwise DEVS May 28 '21 at 05:57
1

We can also use android's splash screen library - link

android {
   compileSdk 31
   ...
}

dependencies {
   ...
   implementation 'androidx.core:core-splashscreen:1.0.0-alpha02'
}

This will give splash screen options in style.xml, you just need to create 2 style.xmls 1 for android api 31 and above and one of below api 31

 <style name="Theme.CustomSplashScreenTheme" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/white</item>
    <item name="windowSplashScreenAnimatedIcon">@drawable/logo</item>
    <item name="windowSplashScreenAnimationDuration">300</item>
    <item name="postSplashScreenTheme">@style/Theme.YourAppTheme</item>
</style>

Learn more about this library using this example

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
  • 4
    Unfortunately, since the introduction of this SplashScreen me and many other people have this issue: when the app is started from VS/Android Studio,there's no icon. What's worse, if in production the app is started through a deep link, still there is no icon, just a blank splash screen. Any idea on how to fix this? – iBobb Jan 18 '22 at 17:05
  • @iBobb I will check your problem and if I found the solution I will update my answer, you can also update my answer if you found the solution. – Rahul Gaur Jan 21 '22 at 08:31
0

you can add this line:

          <item name="android:windowIsTranslucent">true</item>    

in your style.xml file before close style tag. it`s make your default android splash transparent!

Shokie Varun
  • 291
  • 3
  • 6