1. The startup interface is excessively Splash

1. Android 12 has a new SplashScreen API called SplashScreen. The Jetpack framework also includes a library of the same name to accommodate older versions

Advantages: can realize the entry and exit animation and control the display time

Links:

2. Simple :Theme Android :windowBackground

<style name="SplashThemeBase.WithBg">    <item name="android:windowBackground">@drawable/ic_splash_bg</item></style>
Copy the code

2. Enable performance optimization

Reference: application startup time | | Android Developers Android Developers

1. Determine the startup time

1. In Android 4.4 (API level 19) and later, logcat contains an output line that contains a value named Displayed. This value represents the time from starting the process to finishing drawing the corresponding Activity on the screen. The elapsed time includes the following sequence of events:

  1. Start the process.
  2. Initialize the object.
  3. Create and initialize the Activity.
  4. Expand the layout.
  5. First drawing application.

The reported log line looks like the following example:

    ActivityManager: Displayed com.android.myexample/.StartupTiming: +3s534ms
    
Copy the code

2. You can also run the application using the ADB Shell Activity Manager command to measure the initial display time. The following is an example:

adb [-d|-e|-s <serialNumber>] shell am start -S -W
    com.example.app/.MainActivity
    -c android.intent.category.LAUNCHER
    -a android.intent.action.MAIN
Copy the code

The Displayed indicator will appear in the logcat output as before. Your terminal window should also display the following:

Starting: Intent
    Activity: com.example.app/.MainActivity
    ThisTime: 2044
    TotalTime: 2044
    WaitTime: 2054
    Complete
    
Copy the code

-c and -a are optional parameters that allow you to specify

and

for the intent.

3. ReportFullyDrawn () completely displays the elapsed time

You can use [reportFullyDrawn ()] (https://developer.android.google.cn/reference/android/app/Activity#reportFullyDrawn ()) Method measures the time from application startup to full display of all resource and view hierarchies. This data can be useful when applications perform lazy loading. In lazy loading, the application does not block the initial drawing of the window, but asynchronously loads resources and updates the view hierarchy.

If the initial display of the application does not include all resources due to lazy loading, you might consider fully loading and displaying all resources and views as separate metrics: For example, your interface might be fully loaded with some text drawn, but not yet showing images that the application must fetch from the network.

To solve this problem, You can manually call [reportFullyDrawn ()] (https://developer.android.google.cn/reference/android/app/Activity#reportFullyDrawn ()), Let the system know that your Activity has finished lazy loading. When you use this method, Logcat displays values from creation of the application object to invocation [reportFullyDrawn ()] ((https://developer.android.google.cn/reference/android/app/Activity#reportFullyDrawn) at the time. Here is an example of logcat output:

system_process I/ActivityManager: Fully drawn {package}/.MainActivity: +1s54ms
Copy the code

The logcat output sometimes contains the total time, as described in the initial display of elapsed time.

2. Possible problems

1.Application.onCreate()

Running the CPU performance profiler shows, [callApplicationOnCreate()](https://developer.android.google.cn/reference/android/app/Instrumentation#callApplicationOnC Reate (android app. Application)) method finally call you com. Example. CustomApplication. OnCreate method. If the tool shows that these methods are taking a long time to complete execution, you should explore further to see what is going on.

Investigate possible root causes using embedded trace records, including:

  • Initial application[onCreate()](https://developer.android.google.cn/reference/android/app/Application#onCreate())Function.
  • Apply any global singleton object initialized.
  • Any disk I/O, deserialization, or tight loops that may occur during the bottleneck.

Solution:

Lazy initialization: Initializes only objects that are immediately needed. For example, instead of creating global static objects, you move to a singleton pattern where the application initializes objects only the first time they are accessed.

2.Activity.onCreate

Creating an Activity usually requires a lot of expensive work. There are often opportunities to optimize this effort to achieve performance improvements.

Frequently asked questions include:

  • Extend a large or complex layout.

  • Block screen drawing or network I/O on disk.

  • Load and decode bitmaps.

  • Rasterize VectorDrawable objects.

  • Initialize other subsystems of the Activity.

With the Jetpack App Startup development library, we can directly and efficiently initialize components at application Startup time. Developers of both libraries and applications can use this library to simplify the startup process and explicitly set the initialization order. You can use this library to set which components are loaded at what time during startup.

However, App Startup does not load asynchronously and can use third-party librariesAndroid Startup

A typical problem that affects application startup is too much work at initialization. For example, padding layouts that are too large or complex, blocking screen drawing, loading and decoding bitmaps, garbage collection, etc.

  • 1. Do not initialize time-consuming operations in the constructor method, attachBaseContext() and onCreate() methods of the Application. Some data prefetch is carried out in asynchronous threads and can be implemented by Callable.
  • 2, for the initialization of SP, because sp features in the initialization of all the data will be read out in memory, so this initialization in the main thread is not appropriate, but will delay the startup speed of the application, for this or need to be placed in the asynchronous thread processing.
  • 3. For MainActivity, before obtaining the first frame, it needs to measure and draw the contentView layout to reduce the layout level as much as possible. Consider StubView’s delay loading strategy, and of course avoid time-consuming operations in onCreate, onStart and onResume methods.