👨🏼‍💻New Android Splash Screen API and Handling Authentication via Huawei Auth Service

Abdurrahim Çillioğlu
Huawei Developers
Published in
5 min readMay 11, 2022

--

Android Splash Screen

Introduction

Hi everyone, In this article, we’ll build an app from the ground up to explore the new Android Splash Screen. And, we’ll integrate the Huawei Auth Service to take advantage of the authentication system that provides simplicity and security. Also, we’ll use Kotlin as a programming language in Android Studio.

What is the Splash Screen?

Essentially, the Splash screen is the first screen that users will see when the application’s launched the first time. It is used to display branding information like the company logo or name.

Why Should We Use Android Splash Screen API?

When an app is launched, you can see a white blank screen before the main screen is shown. Because there can be some delay between when the user starts your app and loads into memory and Activity’s onCreate() is called. We can improve the user experience by showing our logo and brand instead of the white blank screen.

Without Splash Screen
With Splash Screen

There may be other reasons to use a splash screen, such as downloading data before users start the app or checking the authentication status.

Our Sample App

In this application, we will cover the scenario that users need most: checking authentication and navigation. While showing the splash screen, we will check their authentication status, and navigate to Home Screen or Login Screen page. We can use the Huawei Account Kit and Huawei Auth Service as an authentication system. With Huawei Account Kit, users can sign in to the application with their Huawei ID easily and quickly. Also, users can store their authentication status automatically with the Auth Service.

Our Sample App Flow

1-) HMS Core Integration

We’re not going to go into the details of integrating Huawei HMS Core into a project. You can follow the instructions to integrate HMS Core into your project via official docs or codelab. Also, you can learn all the details from this Medium article.

And, we need to enable Auth Service and Account Kit in AppGallery Connect.

After integrating HMS Core and enabling services, let’s add the necessary dependencies.

2-) Adding Dependency

Add the necessary dependencies to build.gradle (app level).

3-) Adding Theme

Create a style for the splash screen and inherit from “Theme.SplashScreen”. And we can specify various attributes to customize the splash screen app.

If you want to use the animated icon, you should add duration as milliseconds.

<item name="windowSplashScreenAnimationDuration">400</item>

If you want to show a branding image at the bottom of the starting window, you need to add the following item. But please note that this feature requires min API 31 (Android 12).

<item name="android:windowSplashScreenBrandingImage">@drawable/ic_branding_image</item>

My app icon doesn’t display completely. What should I do?

Your icon size should comply with some specifications. The splash screen icon uses the exact specifications as Adaptive icons. Please make sure that your icon not be masked.

4-) Adding Theme in Android Manifest

In the manifest, replace the theme of the starting activity to the theme you created in the previous step (Theme.App.Starting). And add your app theme to your application (Theme.SplashScreenApiApp).

After adding the theme to the manifest file, I’m getting errors like this. What should I do?

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mydemoapp/com.example.mydemoapp.LoginActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

If you add only the SplashScreen theme, you will get this error. Ensure to specify your application theme under the ‘application’ tags or add the theme to every other activity.

5-) Call Splash Screen Inside Entry Activity

Our entry activity (the activity that loads first) should call the installSplashScreen() before the setContentView or other view operations on the root view. Also, we have the setKeepOnScreenCondition function which is for setting the condition to keep the splash screen visible.

Login Screen

Home Screen

Extras

Can I change splash screen animations?

There are two animations on the splash screen. Enter animation and exit animation.
Enter animation is the system view to the splash screen. It is controlled by the system and is not customizable.
Exit animation is the animation run that hides the splash screen. It can be customizable.
You can write your custom animation like this:

I’m using single activity architecture. How should I handle the splash screen? Should I add another splash screen activity?

We don’t need to add another activity for the splash screen. setKeepOnScreenCondition doesn’t block the main thread. We can change the condition according to our authentication system, or something else. Assume that we use ViewModel and authentication.

Tips & Tricks

  • Make sure that the animation duration is no longer than 1 second for a better user experience.
  • Do not avoid using the splash screen library because it is in beta. In the Android official library world, beta means that the library is now stable, and there will be no major changes.
  • Don’t forget to add your splash screen theme to your themes.xml (night) file. Otherwise, the application will crash.

Conclusion

In this article, we have learned how to use the new Splash Screen API. And we’ve developed a sample app that navigates users according to their authentication status. As an authentication system, we integrated the Huawei Account Kit and Huawei Auth Service. I didn’t share with you only the happy path. I have also tried to show you the possible errors you may encounter. Please do not hesitate to ask your questions as a comment.
Thank you for your time and dedication. I hope it was helpful. See you in other articles!

References

Splash Screen Official Documentation

--

--