Android Performance Optimization-accelerating app startup

Source: Internet
Author: User

Android Performance Optimization-accelerating app startup
Application startup Method

Generally, there are two application startup methods in Android: cold start and hot start.

1. Cold Start: When an application is started, the background does not have a process for the application. Then, the system creates a new process and assigns it to the application. This method is cold start.

2. Hot Start: when the application is started, the process of the application already exists in the background (for example, press the back and home keys, although the application will exit, however, the process of the application is still stored in the background and can be viewed in the task list). Therefore, if there are existing processes, such a startup will start the application from the existing process, this method is called Hot Start.

Features

1. Cold Start: Since the system will re-create a new process and assign it to it, the system will first create and initialize the Application class, create and initialize the MainActivity class (including a series of measurements, la S, and la S) and display them on the interface.

2. Hot Start: Because the hot start starts from an existing process, the hot start will not go through the Application step, but directly through the MainActivity (including a series of measurement, layout, and drawing ), therefore, the hot start process only needs to create and initialize a MainActivity, instead of creating and initializing an Application. Because an Application is created from the new process to the destruction of the process, the Application will only be initialized once.

In the above example, you can click the app startup icon to start the app. Another method is to enter the recently used list interface to start the app. Such a method should not be called Startup or recovery.

Application Startup Process

In the Android system, when an application does not have a process, the application starts up like this: When you click the startup icon of the app, the Android system creates a new process from fork in the Zygote process and assigns it to the application, then, the Application class, MainActivity class, windowBackground in Theme of Theme will be created and initialized in sequence to MainActivity, and some properties at the Activity level will be configured, and then the inflate layout and onCreate/ after the onStart/onResume methods are completed, the contentView measure/layout/draw is displayed on the interface, so here, the application is started for the first time. At this time, the interface we see is the first frame.

To sum up, the application startup process is as follows:

Application constructor Method --> attachBaseContext () --> onCreate () --> Activity constructor --> onCreate () --> Configure attributes such as background in the topic --> onStart () --> onResume () --> the measurement layout is displayed on the interface.

Measure the application startup time.

In the above startup process, time-consuming operations anywhere will slow down the startup speed of our application, and the application startup time is measured in milliseconds, for millisecond-level speed measurement, we still need to accurately measure the time it takes to start an application. However, we need to make a measurement based on this time.

What is the application startup time?

Click the application startup icon to create a new process until the first frame of the interface is displayed. This time is the application startup time.

The measurement period can be measured by using the adb shell command. This method is the most accurate and the command is:

adb shell am start -W [packageName]/[packageName.MainActivity]

After successful execution, three measurements are returned:
1. ThisTime: Generally, ThisTime is the same as the TotalTime time, unless a transparent Activity is enabled during application startup to pre-process some things and then display the main Activity, which is smaller than TotalTime.
2. TotalTime: The Application startup time, including the creation process, Application initialization, and Activity initialization.
3. WaitTime: generally greater than TotalTime, including the time consumed by the system.

The following is the measurement of the cold start and hot start time of an application:
Cold start:

Hot Start:

As you can see, when a process already exists, you only need to re-Initialize MainActivity, which is faster to start. However, in most cases, application startup is cold, because the user will manually close the legacy application process in the task list. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KCjxoMiBpZD0 = "reduces the time consumed when the application starts"> reduces the time consumed when the application starts.

For some time consumption during cold start, the Application was measured as a medium-sized app. It took about ms for cold start. If more initialization operations are configured in the Application, this may reach 1 s, so that the latency is obvious at each startup. Therefore, the following policy is adopted during app initialization:
1. Do not initialize time-consuming operations in the constructor method, attachBaseContext (), and onCreate () Methods of the Application. Some data is prefetch in an asynchronous thread and Callable can be implemented.
2. for sp initialization, because sp features will be read out of all data in memory during initialization, this initialization is not suitable in the main thread, on the contrary, it will delay the application startup speed, which still needs to be processed in asynchronous threads.
3. For MainActivity, before obtaining the first frame, you need to plot the measurement layout of contentView to minimize the layout level. Considering the delayed loading policy of StubView, avoid time-consuming operations in the onCreate, onStart, and onResume methods.

Following the above three policies can significantly increase the app startup speed.

Optimized the application startup experience

The application startup time can only be avoided as much time-consuming and unnecessary operations as possible in the main thread, which can reduce the startup time, on the other hand, you can add some configurations to increase the experience while waiting for the first frame to be displayed. For example, if you add the background of the Activity, the background will be displayed on the interface before the first frame is displayed.
1. First, write a theme style for the main interface and set an image to be displayed. Here I set a color and then set it to MainActivity in manifest:

//...                    
                                  
               
          

2. re-set the AppTheme to MainActivity before loading the layout in MainActivity:

@Override    protected void onCreate(Bundle savedInstanceState) {        setTheme(R.style.AppTheme);        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);}        

In this way, the background will be displayed at startup, and then the main interface will be displayed after the page is drawn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.