Android optimization application startup Speed _android

Source: Internet
Author: User

First, the application of the start

Startup mode

In general, there are two types of start-up methods used in Android: Cold start and hot start.

    • 1, cold start: When the application is started, the background does not have the application process, then the system will re-create a new process assigned to the application, this startup mode is cold start.
    • 2. Hot start: When the application is started, the background already has the application process (example: Press the back key, the home key, although the application will exit, but the application process is still kept in the background, can go to the task list to view, so in the case of existing processes, this startup will start the application from the existing process, This way is called hot start.

Characteristics

1, cold start: Cold start because the system will re-create a new process assigned to it, it will first create and initialize the application class, and then create and initialize the Mainactivity class (including a series of measurements, layouts, drawings), and finally display in the interface.

2, hot start: Hot start because it will start from the existing process, so hot start will not walk application this step, but go directly mainactivity (including a series of measurement, Layout, drawing), So the hot start process only needs to create and initialize a mainactivity, without having to create and initialize the application, because one application is created from the new process to the destruction of the process, application is only initialized once.

The above mentioned startup is to click on the app's startup icon to start, and another way is to enter the recently Used list interface to start the application, this should not be called boot, should be called recovery.

Second, the application START process

On the Android system, application in the absence of a process, the application of the startup is such a process: When clicked on the app's startup icon, the Android system will create a new process from the zygote process fork to assign to the application, and then create and initialize the application class, Create the Mainactivity class, load the Windowbackground in the theme style theme, set it to mainactivity, and configure some properties on the activity hierarchy, then inflate the layout, when oncreate/ Onstart/onresume method is finished after the last Contentview Measure/layout/draw displayed in the interface, so until here, the application of the first start is completed, at this time we see the interface is called the first frame.

So, to sum up, the starting process of the application is as follows:

Application constructor Method-->attachbasecontext ()-->oncreate ()-->activity construction Method-->oncreate ()--> configuration properties such as background in the topic- ->onstart ()-->onresume ()--> measurement layout drawing is displayed on the interface.

Third, the measurement of application start-up time

In the above boot process, the time consuming operation in any one place slows down the startup speed of our application, and the application start time is measured in milliseconds, and we still need to measure precisely how long it takes to get the application started.

What is the start time of the application

Start by clicking on the application's startup icon to create a new process until we see the first frame of the interface, which is when the application starts.

What we're going to measure is this time, the measurement is measured in the way the ADB shell commands, which is the most accurate measurement, and the command is:

ADB shell AM start-w [packagename]/[packagename.mainactivity]

After successful execution, three measured times are returned:
1, Thistime: General and TotalTime time, unless the application started with a transparent activity in advance to deal with some things and then show the main event, this will be smaller than the totaltime.
2, TotalTime: Application of the start time, including the creation process +application initialization +activity initialization to the interface display.
3, waittime: Generally larger than totaltime, including the system impact of the time consuming.

The following is the time to measure a cold start and hot start of a application:
Cold start:

Hot start:

You can see that in the case where the process already exists, it is only necessary to reinitialize the mainactivity, which is faster to start, but in most cases the startup of the application is cold startup, because the user will manually close the legacy application process in the task list.

Reduce the time consuming when application starts

For the cold start of some time, such as the application of the test is a medium-sized app, in the cold start time has been faster than 700ms, if the project to a larger point in the application configured more initialization operations, this will likely reach 1s, so that each boot is significantly delayed, So when applying initialization, take the following strategies:

    • 1, in the application constructor method, Attachbasecontext (), OnCreate () method does not carry out time-consuming operation initialization, some data prefetching in the asynchronous thread, can take callable implementation.
    • 2, for the SP initialization, because the SP's characteristics in the initialization of the data are all read out in memory, so this initialization in the main thread is not appropriate, but will delay the application of the start speed, for this or need to put in the asynchronous thread processing.
    • 3, for Mainactivity, because before getting to the first frame, need to contentview the measurement layout of the operation, minimize the layout of the level, consider the Stubview delay loading strategy, of course, in OnCreate, OnStart, Avoid time-consuming operations in the Onresume method.

Following these three strategies can significantly improve the app startup speed.

Optimize the experience when application starts

For the application start time, can only try to avoid some time-consuming, non-essential operations in the main thread, so that the relative can reduce the time to start, in addition to waiting for the first frame, you can add some configuration to increase the experience, such as joining the activity of the background, This background will show up in front of the screen before displaying the first frame.
1, the main interface to write a separate theme style, set a picture to be displayed, here I set a color, and then set to mainactivity in manifest:

<style name= "Apptheme.launcher" >
 <item name= "Android:windowbackground" > @drawable/bule</item >
</style>
/
  ... <activity
   android:name= ". Mainactivity "
   android:label=" @string/app_name "
   android:theme=" @style/apptheme.launcher ">
   < intent-filter>
    <action android:name= "Android.intent.action.MAIN"/> <category
    " Android.intent.category.LAUNCHER "/>
   </intent-filter>
  </activity>

2, and then in the Mainactivity load layout before the Apptheme reset to mainactivity:

@Override
 protected void onCreate (Bundle savedinstancestate) {

  settheme (r.style.apptheme);
  Super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
} 

This will display the background at startup and then display the main interface when the interface is drawn:

This is the entire content of this article, I hope to learn more about Android software programming help.

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.