[Android Performance Optimization Interview Questions] Tell us what cold start and hot start are, the differences, how to optimize, usage scenarios, etc.

Let’s talk about what cold start and hot start are, the difference, how to optimize, usage scenarios, etc.

What do you want to examine with this question?

  1. Do you understand what cold start and hot start are and how they are used in real scenarios? Are you familiar with the essence of cold start and hot start?

Knowledge points to investigate

  1. What are the concepts and basic knowledge of cold start and hot start in projects?

How should candidates answer

Cold start:
 当应用启动时,后台没有该应用的进程,这时系统会重新创建一个新的进程分配给该应用, 这个启动方式就叫做冷启动(后台不存在该应用进程)。冷启动因为系统会重新创建一个新的进程分配给它,所以会先创建和初始化Application类,再创建和初始化MainActivity类(包括一系列的测量、布局、绘制),最后显示在界面上即可
Hot Start:
 当应用已经被打开, 但是被按下返回键、Home键等按键时回到桌面或者是其他程序的时候,再重新打开该app时, 这个方式叫做热启动(后台已经存在该应用进程)。热启动因为会从已有的进程中来启动,所以热启动就不会走Application这步了,而是直接走MainActivity(包括一系列的测量、布局、绘制),所以热启动的过程只需要创建和初始化一个MainActivity就行了,而不必创建和初始化Application
Cold start process

When the user clicks the startup icon of the app, the Android system will create a new process from the Zygote process fork and assign it to the application. Then it will create and initialize the Application class, create the MainActivity class, and load the windowBackground and other attributes in the theme style. Set it to MainActivity and configure some properties at the Activity level, then inflate the layout. After the onCreate/onStart/onResume methods are completed, finally measure/layout/draw the contentView and display it on the interface.

Brief process of the cold start life cycle:
Application construction method–> attachBaseContext()–>onCreate –>Activity construction method–> onCreate() –> Configure the background and other operations in the body –>onStart() –> onResume() –> Measurement, layout, drawing and display

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

第一:减少 onCreate()方法的工作量

第二:不要让 Application 参与业务的操作

第三:不要在 Application 进行耗时操作

第四:不要以静态变量的方式在 Application 保存数据

第五:减少布局的复杂度和层级

第六:减少主线程耗时
Start white screen black screen

Loading the windowBackground and other attributes in the theme style to MainActivity occurs in the inflate layout before the onCreate/onStart/onResume method, and the windowBackground background is set to white or black, so when we enter the first interface of the app, it will cause White screen or black screen for a while before entering the interface

The solution is as follows:

Solution 1: Set the windowBackground background to be the same as the background of the startup page. If your startup page is a picture, you can directly set the picture to the windowBackground attribute and there will be no flash effect.

<style name=``"Splash_Theme"` `parent=``"@android:style/Theme.NoTitleBar"``>`
    <item name=``"android:windowBackground"``>@drawable/splash_bg</item>`
    <item name=``"android:windowNoTitle"``>``true``</item>`
</style>`

Solution 2: Use the world's processing method and set the background to be transparent, giving people a delayed start feeling. , set the background color to a transparent color, so that when the user clicks on the desktop APP picture, he will not "immediately" enter the APP, and will stay on the desktop for a while. In fact, the APP has already been started at this time, but we I deliberately set the color of the windowBackground in the Theme to transparent, and forced the blame to the mobile application manufacturer (the mobile phone response is too slow)

<style name=``"Splash_Theme"` `parent=``"@android:style/Theme.NoTitleBar"``>`
    <item name=``"android:windowIsTranslucent"``>``true``</item>`
    <item name=``"android:windowNoTitle"``>``true``</item>`
</style>`

Solution three: The above two methods appear to be faster visually, but in fact they are just 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, in After the SpashActivity is displayed, it sends a message to the Application to initialize it. This way, the initialization action can be placed later and shorten the time from the application startup to the user seeing the interface.

at last

I have compiled a collection of Android interview questions. In addition to the above interview questions, it also includes [Java basics, collections, multi-threading, virtual machines, reflection, generics, and concurrent programming , Android's four major components, asynchronous tasks and message mechanisms, UI drawing, performance tuning, SDN, third-party frameworks, design patterns, Kotlin, computer network, system startup process, Dart, Flutter, algorithms and data structures, NDK, H. 264, H.265. Audio codec, FFmpeg, OpenMax, OpenCV, OpenGL ES
Insert image description here

Friends in need can scan the QR code below to receive all interview questions + answer analysis for free! ! !

Guess you like

Origin blog.csdn.net/datian1234/article/details/134861012