APP啓動方式分析——冷啓動、熱啓動、溫啓動

APP啓動分析

Cold start

At the beginning of a cold start, the system has three tasks. These tasks are:

1、Loading and launching the app.
2、Displaying a blank starting window for the app immediately after launch
3、.Creating the app process.

As soon as the system creates the app process, the app process is responsible for the next stages:

1、Creating the app object.
2、Launching the main thread.
3、Creating the main activity.
4、Inflating views.
5、Laying out the screen.
6、Performing the initial draw.

系統和應用之間交互簡圖

在這裏插入圖片描述

Application creation

When your application launches, the blank starting window remains on the screen until the system finishes drawing the app for the first time. At that point, the system process swaps out the starting window for your app, allowing the user to start interacting with the app.
If you’ve overloaded Application.onCreate() in your own app, the system invokes the onCreate() method on your app object. Afterwards, the app spawns the main thread, also known as the UI thread, and tasks it with creating your main activity.

From this point, system- and app-level processes proceed in accordance with the app lifecycle stages.

Activity creation

After the app process creates your activity, the activity performs the following operations:

1、Initializes values.
2、Calls constructors.
3、Calls the callback method, such as Activity.onCreate(), appropriate to the current lifecycle state of the activity.

Typically, the onCreate() method has the greatest impact on load time, because it performs the work with the highest overhead: loading and inflating views, and initializing the objects needed for the activity to run.

Hot start

A hot start of your application is much simpler and lower-overhead than a cold start. In a hot start, all the system does is bring your activity to the foreground. If all of your application’s activities are still resident in memory, then the app can avoid having to repeat object initialization, layout inflation, and rendering.However, if some memory has been purged in response to memory trimming events, such as onTrimMemory(), then those objects will need to be recreated in response to the hot start event.
A hot start displays the same on-screen behavior as a cold start scenario: The system process displays a blank screen until the app has finished rendering the activity.

Warm start

A warm start encompasses some subset of the operations that take place during a cold start; at the same time, it represents more overhead than a hot start. There are many potential states that could be considered warm starts.
For instance:

  • The user backs out of your app, but then re-launches it. The process may have continued to run, but the app must recreate the activity from scratch via a call to onCreate().
  • The system evicts your app from memory, and then the user re-launches it. The process and the activity need to be restarted, but the task can benefit somewhat from the saved instance state bundle passed into onCreate().

譯文

冷啓動

簡單講app冷啓動可以分爲兩個階段
第一階段

1、加載並啓動app
2、啓動後立即顯示一個空白的啓動窗口
3、創建app進程

第二階段

1、創建app對象
2、啓動主進程
3、創建MainActivity
4、渲染視圖
5、執行onLayout
6、執行onDraw

應用創建
當應用程序啓動時,空白的開始窗口將保留在屏幕上,直到系統第一次繪製完應用程序。此時,系統進程會爲應用程序交換啓動窗口,允許用戶開始與應用程序交互

之後按照app生命週期依次執行

簡單說就是當屏幕上的組件渲染結束後,用app窗口替換系統空白窗口,此時允許用戶與app進行交互

Activity創建

1、初始化values
2、初始化構造方法
3、執行生命週期回調

熱啓動

熱啓動時,系統將activity放到前臺。如果應用程序的所有activity存在內存中,則應用程序可以避免重複對象初始化、渲染、繪製操作。

如果由於內存不足導致對象被回收,則需要在熱啓動時重建對象,此時與冷啓動時將界面顯示到手機屏幕上一樣。

溫啓動

溫啓動時由於app的進程仍然存在,只執行冷啓動第二階段流程

1、創建app對象
2、啓動主進程
3、創建MainActivity
4、渲染視圖
5、執行onLayout
6、執行onDraw

溫啓動常見場景:

1、用戶雙擊返回鍵退出應用
2、app由於內存不足被回收

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章