A Compendious Guide for the SplashScreenApi

Bhavin Vala
Mindful Engineering
4 min readSep 1, 2022

--

Photo by Jordan on Unsplash

Expressing in Android 12, the splash screen API empowers an application send-off movement for all applications running on gadgets with Android 12 or higher.

A splash screen is a primary screen that you see while opening your application. It likewise implies that something is stacking behind the scenes and you need to sit tight for a couple of moments prior to arriving on the real screen of the application.

How Does the Splash Screen Work?

When a user launches an app while the app’s process is not running or the activity has not been created yet, the following events would occur.

  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.

Designing the Splash Screen in Android 12

The elements of the splash screen are defined by XML resource files in the Android Manifest. There are light and dark mode versions for android 12 and higher.

The customizable elements of a splash screen consist of the app icon, icon background, and window background.

  1. The app icon should be a vector drawable, and it can be static or animated. Although animations can have an unlimited duration, we recommend not exceeding 1,000 milliseconds. By default, the launcher icon is used.
  2. The icon background is optional and is useful if you need more contrast between the icon and the window background. If you use an adaptive icon, its background is displayed if there is enough contrast with the window background, As with adaptive icons, one-third of the foreground is masked.
  3. The window background comprises a single opaque color. On the off chance that the window background is set and is a plain color, it is used naturally in case the characteristic isn’t set.

Splash Screen Aspects

The splash screen icon involves similar details as the Adaptive icon, as follows:

  • Branded picture: This should be 200×80 dp.
  • App icon with an icon background: This should be 240×240 dp, and fit within a circle of 160 dp in diameter.
  • App icon without an icon background: This should be 288×288 dp, and fit within a circle of 192 dp in diameter.

We should investigate how we intend to accomplish this usefulness without composing a ton of standards code along the way.

Coming up next is a representation of what we will accomplish toward this article’s end:

Step 1:- Adding Gradle dependencies

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

Step 2:- Creating a theme

We can plan our splash screen according to the style we set in our application theme. Various characteristics can help us in planning the screen as per our prerequisites.

In the accompanying model, we have set custom animation duration, splash background, and drawable icon. If we don’t determine a symbol here, the screen will show the launcher icon as default.

Step 3: Apply the theme to your activity

Step 4: Triggering Splash screen before the Home Screen

To set off the splash screen just before the home screen, we have to call an extension function: installSplashScreen() before setting the content view for the activity.

How to Manage Pre-Fetch Data Functionality With SplashScreenApi?

In the old way, we were manually customizing the splash screen and pre-fetched the data. But with SplashScreenApi we only need to pre-fetch data.

Here is the new way to pre-fetch the data and navigation accordingly.

To fetch your required data make API calls inside the onCreate() method. Use onPreDrawListener() to wait for data to be loaded.

Add an OnPreDrawListener to the root view. When triggered, if isContentLoaded is true, then it will remove itself as a listener and returns true, allowing the app to start drawing its views. If isContentLoaded is false, meaning the content is not ready yet, it delays the app from drawing. Here, isContentLoaded is a local variable set in ViewModel after a delay, it’ll redirect you to the main screen flow.

Handle Navigation Conditionally

We are handling navigation according to network call response. In the above example if isCompleted is true then we’ll start the main screen flow.

And that’s all required to implement SplashScreenApi.

Clap 👏 If this article helps you. Happy coding, Happy reading ✨

--

--