When testing the application today, I found that before entering the splash screen, there would be a white screen or a black screen for 1-2 seconds (color depends on the theme of the setting).

I. This article mainly solves the following problems:
  1. White screen/black screen when APP starts and white screen/black screen when Activity starts.
  2. APP startup speed is slow, how to realize the APP starts in seconds after clicking ICON? APP startup acceleration.
Two, understand the drawing of the entire window:
  1. Draw the background.
  2. Draw the contents of the View itself.
  3. Draw the child View.
  4. Draws modifiers (such as scroll bars).

StartingWindow (Preview Window)

Three, common practices:

In normal development, we call setContentView(View) in the onCreate() method of an Activity to set the display layout of the Activity.

When you start an Activity, if the Activity’s Application is not already running, the system creates a process for the Activity. Application onCreate() may be called multiple times), but the process takes time to create and initialize. If the initialization takes too long, nothing will happen on the screen until the action is complete, and the user will think that the button was not clicked. So what can I do if I can’t stop in the same place and I’m not showing a new interface? This gives the appearance of StartingWindow (also known as PreviewWindow), which makes it look like the Activity is already started, but the data content has not been initialized.

StartingWindow usually appears before the application process is successfully created and initialized, so it is a temporary window and the corresponding WindowType is TYPE_APPLICATION_STARTING. The goal is to tell the user that the system has received the action and is responding, implement the target UI after program initialization, and remove the window.

StartingWindow is the main cause of the white screen and black screen we will discuss. Normally, we will set the Theme for the Application and Activity, and the system will initialize StartingWindow based on the Theme. At the top of the Window layout is a DecorView. StartingWindow displays an empty DecorView, but applies the Activity Theme to the DecorView. If the Activity does not specify a Theme, use the Application (the Application system requires a Theme).

Iv. Solutions

1. Set a transparent theme for the Activity page

<style name="SplashTheme" parent="AppTheme">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsTranslucent">true</item>
</style>
Copy the code

When the APP and Activity are started, our StartingWindow will apply the theme with the transparent background. It is true that there is no white screen or black screen when jumping, but this setting will have the following consequences: 1.1. After setting up SplashActivity, users need to wait about 2 seconds for the contentView to be displayed after clicking our APP icon. This creates the illusion that the APP starts slowly. The Activity is already started, but the background is transparent, and clicking anywhere else on the desktop is invalid. This is exactly the opposite of what Google is trying to do, so keep reading. After 1.2, to other Activity sets, can lead to a closed by a overridePendingTransition set to start the Activity of animation is invalid. The following animations need to be rewritten in style:

<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowAnimationStyle">@style/Animation.Activity.Translucent.Style</item>
<item name="android:windowFullscreen">true...
<item name="android:windowIsTranslucent">true...
</style>

<style name="Animation.Activity.Style" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">...
<item name="android:activityOpenExitAnimation">...
<item name="android:activityCloseEnterAnimation">...
<item name="android:activityCloseExitAnimation">...
</style>

<style name="Animation.Activity.Translucent.Style" parent="@android:style/Animation.Translucent"> 
<item name="android:windowEnterAnimation">...
<item name="android:windowExitAnimation">...
</style>
Copy the code

1.3. Jumping between activities may occasionally see the desktop flash by (if SplashActivity and other activities are set to transparent).

Summary: Generally, only a transparent background theme is set for SplashActivities. Other activities do not have a transparent background theme. This is the best experience. However, if you want to achieve the APP seconds on or not, which is contrary to the principle we analyzed at the beginning of the article.

2. To achieve the effect of turning on in seconds, we set the Window transparency to remove the white screen and black screen. Now we need to create a color or picture to replace the white screen and black screen, so first we need to remove the transparent attribute in the original style. Then set a background color or image for the Window.

To implement step 2.1, create a layer-list under res/drawable with any name such as splash

<? xml version="1.0" encoding="utf-8"? > <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <! <item Android :drawable="@color/white"/> <item> <! <bitmap Android :gravity="center"
            android:src="@drawable/wel_page" />
    </item>
</layer-list>
Copy the code

Layer-list, I think you can write it, you have the background color at the top, and then you have an image at the bottom, and it can be a full-screen image, it can be a small image. If it is a full screen image, the color of the image can be left unset. if it is a small image, the color can be specified, and the position of the image can be specified.

2.2 Setting the Background for the Window

<style name="SplashTheme" parent="AppBaseTheme"> <! -- Welcome page background reference just written --> <item name="android:windowBackground">@drawable/splash</item>
    <item name="android:windowFullscreen">true</item> <! -- <item name="android:windowIsTranslucent">true</item> --> <! </style>Copy the code


can use our layer-list as the background, of course can also set a full screen image.

2.3. In androidmanifest.xml, define the SplashActivity theme as SplashTheme:

<activity android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
</activity>
Copy the code

2.4. To implement SplashActivity, start your MainActivity on onCreate() and do nothing else:

public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); startActivity(new Intent(this, MainActivity.class)); finish(); }}Copy the code