A few things to understand before reading this article: 1. What is Cold startup time for Android? Cold start time refers to the time between the moment the user clicks on the APP on the mobile desktop and the onCreate() method called by the Activity that launches the page. 2. What happens during the cold start period? First we need to know what happens when an Activity is started. When an Activity is started, if the Application to which the Activity belongs has not yet been started, The system creates a process for the Activity (Application is called once for each process created, so its onCreate() method may be called multiple times). During the process creation and initialization, it takes some time. WindowManager will first load the windowBackground in the theme style of the APP as a preview element, and then go to the actual loading layout. If this takes too long, and the default background is black or white, it will give users the illusion that the APP is very slow and not smooth. Naturally, it also affects the user experience.

Take a look at the effect:





Not optimized





Optimization Scheme 1





Optimization Scheme 2

First, eliminate the white screen/black screen during startup

When the user clicks on the mobile desktop APP, the black screen or white screen is actually the first frame before the interface rendering. If you understand the two problems in the header of the article, then it is very easy to solve this problem. You just need to set the windowBackground in the Theme to the picture we want the user to see. There are two ways to do this:

1. Set the background image as the Logo of our APP as the guide for the launch of the APP, which is what most apps in the market do now.

    <style name="AppWelcome" parent="AppTheme">
        <item name="android:windowBackground">@mipmap/bg_welcome_start</item>
    </style>Copy the code

2. Set the background color to transparent, so that when the user clicks on the desktop APP image, it will not “immediately” enter the APP and stay on the desktop for a while. In fact, the APP is already started, but we deliberately set the windowBackground color in the Theme to transparent. Forced to blame the mobile phone application manufacturers (mobile phone reaction is too slow, ha ha), in fact, now wechat is also doing so, you can try.

    <style name="Appwelcome" parent="android:Theme.Translucent.NoTitleBar.Fullscreen"/>Copy the code

Note that if you introduce the Theme directly into your Activity, you may get the following exception:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

This is due to the use of incompatible Theme, for example I have an Activity that inherits AppCompatActivity, the solution is very simple: Set the Theme of our original APP before super.oncreate (savedInstanceState) in the onCreate() method

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            setTheme(R.style.AppTheme);
            super.onCreate(savedInstanceState); }}Copy the code

In both cases, we need to introduce the Theme into the corresponding Activity

        <activity
            android:name=".app.main.MainActivity"
            android:theme="@style/AppWelcome"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>Copy the code

Second, about startup optimization

In the process of creating an Activity, it actually goes through a series of operations on the framework layer. In daily development, we will have to rewrite the Application class. Then perform some initialization operations in the Application, such as static TOKEN to store user id, initialization of third-party SDK, etc. Here are a few suggestions: 2. Do not perform time-consuming operations in Application. For example, some developers may store a series of folders or files in their APP (like myself). 3. Do not store data in Application as static variables.

Of course, this is absolute idealism, the above “do not” before the 2 word “try to “2 words, after all, in the actual development, this will make us a lot more convenient.

In addition, layout is also very important, try to reduce the complexity of layout, layout depth, because in the View drawing process, measurement is also very expensive performance.