Solution of black screen and white screen when Android starts APP

  • 2021-09-20 21:25:58
  • OfStack

In development, when we start app, the screen will appear white screen or black screen for a period of time, and different devices will have different time lengths. It affects the user experience.

First of all, analyze the reasons for this phenomenon. When we start an application, the system will check whether such a process already exists. If it does not exist, it is a cold start. Both the system and APP itself have a lot of work to do. First, the service of the system will check the information of intent in startActivity, then create the process, and finally execute the operation of starting Acitivy. The problem of displaying white and black screens mentioned above came into being during this period of time.

Before drawing the page and loading the layout, the system will first initialize the window (Window), and during this step, the system will specify its Theme theme color according to the Theme we set. At the top level of Window layout, DecorView and StartingWindow display an empty DecorView, and our settings in Style determine whether a white screen or a black screen is displayed.

The simplest solution

Since the black and white screen is determined according to the Theme we set, we can solve this problem directly from the Theme of the startup page.

Step 1: Define the following in the style file


<!--*************** Launch page Theme***************-->

 <style name="Theme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="windowNoTitle">true</item>
  <item name="android:windowContentOverlay">@null</item>
  <item name="android:windowBackground">@drawable/splash_pic</item>
  <item name="android:windowFullscreen">true</item>
</style>

Step 2: Set the startup page Theme in AndroidManifest


<activity
   android:name=".ui.SplashActivity"
   android:theme="@style/Theme.Splash"
   android:screenOrientation="portrait">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>

As above, we set a full-screen background image for the startup page, so that the default theme color is not displayed.

According to the above configuration, the problem of white screen or black screen can be avoided.


Related articles: