Android performance optimization-App startup speed optimization

Insert picture description here

White screen and black screen when the app starts

During development, setContentView(View) will be called in the onCreate() method of the Activity to set the layout of the Activity, then the problem is here, since we set the layout, why is there a white screen or a black screen at startup instead of displaying the set layout What? Let's analyze the reasons together below.
When opening an Activity, if the Application to which the Activity belongs is not already running, the system will create a process for the Activity (Each process is opened, there will be an Application, so Application's onCreate() may be called multiple times, one The process is called once), but it takes time to create and initialize the process. If the initialization takes too long before this action is completed, there may not be any movement on the screen, and the user will think that the button is not clicked. So what should I do if I can't stop at the original place without displaying the new interface? This has the appearance of StartingWindow (also called PreviewWindow), which looks like the Activity has been started, but the data content has not been initialized.
Source code:
when starting the root activity: ActivityStack#startActivityLocked -> showStartingWindow

StartingWindow generally appears before the application process is created and initialized successfully, so it is a temporary window, and the corresponding WindowType is TYPE_APPLICATION_STARTING. The purpose is to tell the user that the system has accepted the operation, is responding, realizes the purpose UI after the program is initialized, and removes this window at the same time.

This StartingWindow is the root cause of the white screen and the black screen. Generally, the developer will set the Theme for the Application and Activity, and the system will initialize the StartingWindow according to the set Theme. The top level in the Window is DecorView, StartingWindow displays an empty DecorView, but will apply the Theme specified by this Activity to this DecorView. If the Activity does not specify the Theme, then the Application will be used (Application is a system requirement that the Theme must be set).

In the Theme, you can specify the background of the window, the ICON of the Activity, the overall text color of the APP, etc. If no attributes are specified, the default attributes will be used, which is the empty DecorView mentioned above, so our white screen and black screen It is closely related to empty DecorView. The Style we set for the APP determines whether it is a white screen or a black screen.

1. If the theme of the Black series is selected, the screen will be black when Activity jumps:

@android:style/Theme.Black"

2. If you choose the theme of the Light series, the screen will be blank when Activity jumps:

@android:style/Theme.Light"

Solution

1. Set the background image Theme
by setting a background image. When the program starts, first display this background image to avoid a black screen

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:screenOrientation">portrait</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">>@mipmap/splash</item>
        <item name="android:windowIsTranslucent">true</item>        
</style>

2. Set transparent Theme
By setting the style to transparent, the screen will not be black after the program is started, but the whole will be transparent. It will be displayed at one time after the interface is initialized.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  		<item name="android:screenOrientation">portrait</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsTranslucent">true</item>      
</style>

Comparison of the two: the
first: the program starts quickly, the interface first displays the background image, and then refresh other interface controls. However, if the background image and the style of the page to be displayed are very different, it will give the user a feeling that the refresh is out of sync.
The first type: to give the user a feeling that the program starts slowly, the interface is flashed out once, refreshed and synchronized.

3. Remove PreviewWindow

    <style name="AppStarTheme" parent="AppTheme">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@null</item>
        <item name="android:windowDisablePreview">true</item>
    </style>

It will cause the user to mistakenly think that there is no click feeling after clicking the app icon on the desktop

Guess you like

Origin blog.csdn.net/yzpbright/article/details/109136679