1.app cold start

app cold start: When the application is started, there is no application process in the background, then the system will recreate a new process to assign to the application, this startup method is called cold start (there is no application process in the background). Because the system will recreate a new process to assign to it, it will first create and initialize the Application class, then create and initialize the MainActivity class (including a series of measurement, layout, drawing), and finally display it on the interface.

2. app hot start

App hot start: When the app has been opened, but returned to the desktop or other programs when the back button or home button is pressed, then the app is reopened, this is called hot start (the app process already exists in the background). Because the hot start will be started from the existing process, so the hot start will not go to the Application step, but directly to the MainActivity (including a series of measurement, layout, drawing), so the hot start process only needs to create and initialize a MainActivity on the line, instead of creating and initializing the Application

3.The process of cold start

When you click the app launch icon, Android system will fork a new process from Zygote process and assign it to the app, then it will create and initialize the Application class, create the MainActivity class, load the windowBackground and other properties in the theme style Theme and set it to MainActivity and configure some properties in the Activity hierarchy, then inflate the layout, when the onCreate/onStart/onResume methods have been completed, and finally the contentView measure/layout/draw is displayed on the interface.

4. Brief flow of the cold start life cycle:

Application constructor -> attachBaseContext() -> onCreate -> Activity constructor -> onCreate() -> configure the background and other operations in the body -> onStart() -> onResume() -> measure, layout, draw display

The optimization of cold start is mainly visual optimization to solve the problem of white screen and improve user experience, so through the process of cold start above. The optimizations that can be done are as follows:

Reduce the workload of onCreate() method

Don’t let the Application participate in business operations

Do not perform time-consuming operations on the Application

Do not store data in the Application as static variables

Reduce layout complexity and hierarchy

Reduce main thread time consumption

5. Other problems and solutions of cold boot

Why is there a white screen black screen problem in cold start? The reason is that the properties such as windowBackground set to MainActivity in the theme style Theme are set in the flate layout before the onCreate/onStart/onResume methods, and the background of windowBackground is set to white or black, so we When we enter the first interface of the app, it will cause a white screen or black screen before entering the interface. The solution is as follows

  1. set the windowBackground background to the same as the background of the start page, if your start page is a picture then you can directly set the windowBackground property to the picture then there will not be a flashing effect
1
2
3
4
5
6
7
<style name=``"Splash_Theme"` `parent=``"@android:style/Theme.NoTitleBar"``>`

<item name=``"android:windowBackground"``>@drawable/splash_bg</item>`

<item name=``"android:windowNoTitle"``>``true``</item>`

</style>
  1. Adopt the world’s processing method, set the background to be transparent, to give people a sense of delayed launch. The background color is set to transparent, so when the user clicks on the desktop APP picture, it will not “immediately” enter the APP, and stay on the desktop for a while, in fact, the APP is already launched, but we have set the color of the windowBackground in the Theme to transparent, forcing the pot to the mobile application manufacturer (the phone reaction too slow)
1
2
3
4
5
6
7
<style name=``"Splash_Theme"` `parent=``"@android:style/Theme.NoTitleBar"``>`

<item name=``"android:windowIsTranslucent"``>``true``</item>`

<item name=``"android:windowNoTitle"``>``true``</item>`

</style>
  1. The above two methods are visually appear faster, but in fact it is only an appearance to make the application start faster, there is an idea to implement lazy loading of unnecessary initialization actions in the Application, for example, sending a message to the Application after the SpashActivity is displayed to initialize it, so that the initialization action can be put on the back This will shorten the time between the start of the application and the user seeing the interface.