Implementation of app Startup Optimization for Android Optimization

  • 2021-09-16 08:01:00
  • OfStack

App startup mode

Cold start

If App has not started or App process is killed, the App process does not exist in the system, and starting at this time is a cold start. It is necessary to create App process, load related resources, start Main Thread, initialize the first screen Activity, etc. During this process, the screen displays a blank window (color based on theme) until the first screen Activity is fully started.

Hot start

Hot boot means that the App process is only in the background, and the system just takes it from the background to the foreground to show it to the user. Similar to the cold start, during this process, the screen displays a blank window (color based on theme) until activity is rendered.

Warm start

Between cold start and hot start, 1 generally occurs in the following two situations:

The user returns to exit App and then starts again. The process may still be running, but activity needs to be rebuilt. After the user exits App, the system may kill App due to memory reasons, and both the process and activity need to be restarted, but the state of passive kill lock (saved instance state) can be restored in onCreate.

Start-up time detection

adb

adb shell am start -W [Apply Enrollment]/[Full Pathname of Activity] and get 3 time values:

1. ThisTime

1 is the same as TotalTime, if the application boot opens a transition to fully transparent page preprocessing 1 things, this will be smaller than TotalTime.

2. TotalTime

Apply startup time, including process creation, Application initialization, Activity initialization to display.

3. WaitTime

1 is larger than TotalTime, including the time consumption of the system.

Code dot

attachBaseContext of Application is called when the application process is first created, which can be used as the starting dot of cold start.

Optimization

1. Layout optimization

2. Logic optimization

Necessary and time-consuming logic, considering separate open thread execution It is necessary and time-consuming, and it is executed in turn according to priority Unnecessary delayed initialization, such as re-initialization when used

3. Deception effect against cold start

Using placeholder UI

Android the latest Material Design recommends using one placeholder UI to show users until App is loaded, similar to iOS.

Customize the theme, set the windowBackground property, and add background to Window.


<style name="SplashTheme" parent="AppTheme">
  <item name="android:windowBackground">@drawable/logo_splash</item>
</style>

First load an Activity without rendering layout as the startup screen

Write an LogoSplashActivity that does nothing


public class LogoSplashActivity extends BaseActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //  There is no such thing as  setContentView Is simply used to jump to the corresponding  Activity
    //  The purpose is to reduce the first screen rendering 
    if (AppPref.isFirstRunning(this)) {
      IntroduceActivity.launch(this);
    } else {
      MainActivity.launch(this);
    }
    finish();
  }
}

Set it as the boot screen in AndroidManifest. xml and add a theme


<activity
  android:name=".ui.module.main.LogoSplashActivity"
  android:screenOrientation="portrait"
  android:theme="@style/SplashTheme">
  <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>
</activity>

In this way, the boot screen displays LogoSplashActivity, does not render the layout itself, and displays placehold UI background of theme setting.


Related articles: