1. Visual optimization

1.1. Basic concepts

On cold startup, the system has three tasks:

  • Load and start the application
  • Displays a blank application startup window immediately after startup
  • Create the application process

Once the application process is created, it takes care of the next phase:

  • Creating an APP object
  • Start main thread
  • Create an Activity object for the application entry
  • Populates the Views of the loading layout
  • Perform the View’s drawing process, measure-layout-draw, on the screen

After the first drawing, the system process swaps the currently displayed background window and replaces it with the main Activity before the user can actually use the application

Visual optimization is to set the theme of blank startup window in the first and second stages of cold startup to make the black and white screen disappear from the visual level, but it does not improve the startup speed of APP

1.2. Default

If nothing is done to the APP, that is, the default theme is used, you will have a black and white screen. The launch window disappears around the time of the onWindowFocusChanged callback

1.3. Transparent theme optimization

Set a transparent background theme for Application, for example

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsTranslucent">true</item>
</style>            
Copy the code

Although there is no white screen, there is still a visual delay when you click the APP icon to display it on the first page

1.4. Picture theme

Set the subject of a splash image for Application, for example

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/lunch</item>
    <item name="android:windowFullscreen">true</item>
    <! -- Display virtual buttons and free up space -->
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>
Copy the code

When set up like this, if the splash screen picture and the splash screen Activity look exactly the same, they work seamlessly.


2. Code optimization

For visual optimization, the fundamental solution is to optimize the code

2.1. Statistics of cold start time

2.1.1. Method 1: ADB

Adb shell am start -s -w Specifies the package name/class full path qualified name. -s means to restart the current application

dst07130:~ renpeng$ adb shell am start -S -W com.pren.androidtest/com.pren.androidtest.MainActivity
Stopping: com.pren.androidtest
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.pren.androidtest/.MainActivity }
Status: ok
Activity: com.pren.androidtest/.MainActivity
ThisTime: 594
TotalTime: 594
WaitTime: 638
Complete
Copy the code

The meanings of the three times:

  • ThisTime: start time of the last Activity, i.e. the Activity entered in the command (Cold start optimization concerns)
  • TotalTime: the TotalTime it took to start a sequence of activities
  • WaitTime: Time taken to create the application process + TotalTime (user awareness)

2.1.2. Method 2: Logcat

Logcat Enter Display to filter system logs without filtering information No Filters and level Verbose.

The time displayed is ThisTime

2.2. Application optimization

Many third-party components are initialized first in the onCreact of the Application

However, heavy initialization operations and complex logic in the Application will affect startup performance. For example:

  • Complex and tedious initialization layouts
  • Operations that block the main thread, such as I/O, databases, etc
  • Load a Bitmap or VectorDrawable, etc
  • Other operations that occupy the main thread

Optimizations generally fall into three categories:

  • Only on the main thread and must be executed immediately — no optimizations
  • Only on the main thread but not immediately – delay
handler.postDelayed(new Runnable() {
        @Override
        public void run(a) { / *... * /}},3000);
Copy the code
  • You can not execute a child thread in the main thread
new Thread(new Runnable() {
        @Override
        public void run(a) {
            // Set the priority of the thread without competing with the main thread
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            // Child thread delay execution, such as Bugly, X5, SP, UmENG, etc initialization
            // Suggest delay to see if it affects other functions or crashes!
            Thread.sleep(5000);
        }
    }).start();
Copy the code

2.3. Optimization of splash screen

Those that are only on the main thread and must be executed immediately and cannot be optimized can be offset by adding a splash screen page

Total target screen time = Blank page time + Real screen time

Once the Application is initialized, the attachBaseContext method (which is the first callback in the entire APP) is called, followed by the onCreate method of the Application, so the startup time can be recorded in the Application.

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    // You can use SP, or you can use global variables to record the start time
    long startTime = System.currentTimeMillis();
}
Copy the code

The onWindowFocusChanged method will be called after the Activity gets the user’s touch event and View drawing (the first frame receipt is completed, but it does not mean that the data is displayed), so the APP startup time can be calculated.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    // The time from application to entry Acitity
    long diffTime = System.currentTimeMillis() - startTime;
    // Therefore, the time displayed on the splash screen is the total time of the target splash screen - diffTime.
}
Copy the code

In this way, the display time of the flash screen can be dynamically set, so that the mobile phone with different performance can keep the same display time as far as possible.