Add Splash Screen in KMP Compose Multiplatform App

Today’s tutorial will teach us to integrate splash screens in KMM Android & iOS applications. This tutorial will discuss all the step-by-step guides for adding a splash screen in KMP with the shared commonMain code.

Add Splash Screen in KMP Compose Multiplatform App

Add Splash Screen in KMP Compose Multiplatform App

A Splash screen is the first screen that appears on a mobile application during app launch time for a few seconds. This screen contains a small description of the app in visualization form. This screen can contain Text Images or app logos as well. The splash screen helps us reduce the app data loading time when loading data from APIs. When displaying the splash screen, we start loading data from the internet into our application.

1. I have designed a demo splash screen image for this tutorial, which we will use in this tutorial. Download the image from below.

Sample Splash Screen

2. Paste the splash screen image into

composeApp -> commonMain -> composeResource -> drawable folder.
Splash Screen Shared Resources Folder
3. After putting together the image, We have to Rebuild our project.
Rebuild KMP project
4. Create a Kotlin file named SplashScreen.kt in Kotlin’s common main shared folder.
5. Source code of SplashScreen.kt file.

Code explanation:

  • Our main composable SplashScreen has an argument onTimeout which returns nothing. In this argument, we will execute a function passed from our main screen to execute in this screen.
  • The LaunchedEffect function executes every time a composable executes. In this function, we add a delay of 2 seconds in milliseconds.
  • The Image widget is our Splash screen image.

6. Code for App.kt file.

Code explanation:

  • When our main App function starts executing, then first it checks Splash screen is visible or not using mutable state management.
  • First, it opens the Splash screen, and after 2 seconds, it hides the splash screen and loads our main HomeScreen.

Live screenshot:

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *