Know what you are and why you are

If you don’t know anything, you can check your documentation to find out exactly what happens to your system and applications at startup, and what they do in those states

Three states of App startup

Each of these states affects how long your App is visible to users

Cold start (Cold start is the state in which the App starts from the zero state. Compared with other states, we should pay more attention to optimizing the start in this state)

What is cold start?

The device starts for the first time when the system closes the App

What does the system do on a cold start?
  1. Load and start the application
  2. After startup, a blank startup window for the application is displayed
  3. Create app process
  4. App Process starts to do the next part (see figure below)

Hot start (Hot start and warm start are the state in which the system brings the running App from the background to the foreground)

Restart the App, but the App’s Activitys are still in memory

Wen started

If I re-enter my App, and I call onCreate() again, that’s warm startup, and then I call onCreate, and I think the App data is wiped out in memory

Here can be analyzed first, according to the official website provided by the figure, pay attention to look, in this single day, we can not change the source of the system, we can only do the point of attention in our development,Two onCreate callback methodsWe are sure to use it at ordinary times. Do not do too much time consuming here and affect DislayTime

App startup process analysis

I read in Shaven’s Android Development Master class column that our real business is more than this simple, we may have a splash screen page, advertising page, and then to the home page, which is the only page display, loading ads, hot fix frame, plug-in talk frame, etc

Viewing startup Time

So you can go to logcat Displayed to see how long you’ve applied to a page

The 2019-07-26 14:36:40. 016, 1151-1188 /? I/ActivityManager: Displayed com.txt.demo/DemoActivity: +847ms (total +2s405ms)Copy the code

Or view it from the ADB command line

adb shell am start -S -W com.txt.demo/DemoActivity
Stopping: com.txt.demo
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.txt.demo/DemoActivity }
Status: ok
Activity: com.txt.demo/DemoActivity
ThisTime: 405
TotalTime: 908
WaitTime: 939
Complete
Copy the code

You can also call reportFullyDrawn(), which is the Activity method, to see how long it took to get to that page. Depending on the needs of your project, for example, if you have a series of lazy loads that the user can’t click on, you can call this function after lazy loads, tell the system, and then you can view.

The 2019-07-26 14:52:23. 990, 1151-1485 /? I/ActivityManager: Fully drawn com.txt.homecredit/.ui.MainActivity: +1s380msCopy the code

Of course, good tools are important,CPU analyzer for ASTools really need to know at least one, because a lot of performance analysis, find ideas or want to find problems, tools can help you to add wings, I have time to share

Possible startup problems

1. Click the icon does not respond for a long time, or there is a white screen (black screen) if we do not process the application, there will be this problem, to solve this problem, there are many online solutions, some solutions say to disable the preview window, but the official website says that it is recommended to set the background image method theme

<? xml version="1.0" encoding="utf-8"? > <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap android:src="@mipmap/app_bg"
            android:gravity="fill"/>
    </item>
</layer-list>
Copy the code

Now there are many businesses, and the startup process is more and more complicated. Splash screen advertisement, hot repair frame, plug-in talk frame and so on all need to be completed in the startup stage, so it will lead to the slow display of some models with poor performance. This operation is based on the home page display too slow to do optimization, more work in asynchronous processing, but the disadvantages caused by the page display, some data did not come out, resulting in a white screen, or users can not operate the page.

Start the optimization

  • Application optimization in the cold start diagram above, you can see that onCreate() does as little time-consuming work as possible
  • Flash optimization Flash optimization, is the preview window to achieve the effect of the flash screen, you can use the method I said above to achieve
  • During the startup process of business optimization, we need to know which modules must be used and which can be loaded later
  • Details of the optimization
    1. For example, in the advertisement page, the image needs to be displayed for 3 seconds. When obtaining the image, if it is more than 3 seconds, it can be displayed next time (at this time, the previous image can be displayed or not displayed, and the logo can be displayed). If it is within 3 seconds, the image of this time will be displayed.
    2. The module that must be loaded mentioned above can be optimized again. Can it be realized by algorithm or other ways? It is an excellent point (this part feels the ability of key points, haha, I am following Shaowen in learning).
    3. Threads, GC, and so on can all be optimized

Reference in the development of the road, will never be eliminated is the performance problem, thanks to Shao Wenshen

If you have a problem, turn to Google for documentation

Startup optimization, we need to know the logic of the system from startup to display page, we need to clearly understand our application, business in the startup process to do, most of the optimization we can do, is in our business, through better implementation optimization.