If you have had any smart phone before the I am sure you have seen a splash screen before. If you have not then for your information a spalsh screen is a screen that displays when you first open an app on the device. Many developer may refers to this as a launch screen and displays when that app is loading after being opened. When this loading process has completed, it transitions to a different screen where actual actions can be performed.
If you have noticed these splash sceen you most definitely noticed that they tend to only display for a short time and then its gone. Personally I feel that the splash screen is pretty vital part to any application since it is the user’s first impression/experience with the application.
Implementing Splash Screens
There are technically two ways to implement a splash screen.
Using a Timer (Get It Together)
This is the old easy approach. You have to create a dedicated splash screen Activity that shows up for x seconds then opens the appropriate activity. You get more flexibility here as you can add animations, custom views or any other element you can normally fit into an Activity layout. A very basic implementation of this is below
class SplashActivity : AppCompatActivity() { //setting the timer for the activity private val SPLASH_TIMER:Long = 5000 // this equates to seconds override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) //fire after timer expires timedSplashScreen() } private fun timedSplashScreen() { Handler().postDelayed({ //start the main activity startActivity(Intent(this, MainActivity::class.java)) }, SPLASH_TIMER) } }
Advantages:
- You can display awesome animation or some custom design that has been built. For example, the development of games.
- perform alternative activities on the splash screen
Disadvantages
- the launcher activity doesn’t show up immediately
- this is even worst during a cold start
- additionally, during cold start the user is stuck looking at the
windowBackground
- afterward, the user still waits until the splash screen time expires before the app content
- Don’t expect the animation to wow your user every time
Using a Smart Timer (Get It Together)
This is very similar to the timer method listed above. The difference here is rather than make the delay be fixed, you vary it based on whether this is the user’s first time launching the app or not. We can accomplish this by using the SharedPreferences
.
class SmartSplashActvity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_smart_splash_actvity) splashScreenKey() } private fun splashScreenKey() { val sp = getPreferences(MODE_PRIVATE) val firstLaunchPrefKey = "pref_first_launch" val splashDuration = when (sp.getBoolean(firstLaunchPrefKey, true)) { true -> { // sp.edit().putBoolean(firstLaunchPrefKey, false).apply() 5000 } false -> { // 10000 } } splashScreenDuration(splashDuration) } private fun splashScreenDuration(splashDuration: Int) { Handler().postDelayed({ startActivity(Intent(this, MainActivity::class.java)) }, splashDuration.toLong()) } }
Advantages:
- All the advantages that timer accomplished
- this method could aid in getting to the content quicker to the user.
Disadvantages
- all the disadvantages that exist for timer method
Splash Screen Best Practice
Now doing what needs to be done the right way. When the app is launched and has been in the memory yet, there is a delay between when the user started your app and when the launcher Activity’s onCreate()
is called. During this what we call a “cold start”, the window manager tries to draw a UI placeholder using elements from the theme.xml. The key is creating a custom theme that overrides windowBackground
, then replacing that custom theme with your standard theme before calling super.onCreate()
in the activity.
class DedicatedSplashScreen : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) startActivity(Intent(this, MainActivity::class.java)) finish() } }
- Keep it free from unnecessary distraction
- Don’t use multiple colors or logos
- Use animation sparingly
Splash screens are simple. They’re used to enhance a brand and give users something nice to look at as they wait. .