Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

Android App 啟動流程分析與優化實務

編輯:Android 資訊

從一次加班說起

新版本終於發布了,但測試仍舊回報:App 啟動要等好久才進到首頁。於是今天來把 App 啟動這件事好好梳理一遍。

兩種啟動方式

冷啟動

啟動時背景中沒有該 App 的進程,系統必須重新建立一個進程分配給它。這條路徑會先建立並初始化 Application,接著建立並初始化 MainActivity(包含測量、布局、繪製一連串流程),最後才顯示在畫面上。

熱啟動

啟動時該 App 的進程已經存在——按 BACK 或 HOME 離開後,進程通常仍留在背景,可以到最近任務清單中確認。此時會直接從既有進程啟動,因此不會再走 Application,而是直接進入 MainActivity。

原因很簡單:一個 App 從進程建立到銷毀,Application 只會初始化一次。

完整啟動流程

綜合兩種方式,冷啟動的順序是:

Application 建構子 → attachBaseContext() → onCreate() → Activity 建構子 → onCreate() → 套用主題背景等屬性 → onStart() → onResume() → 測量布局繪製並顯示

可以優化的地方

依上面的流程,原則有三條:

  1. Application 的建立過程中盡量少做耗時操作;
  2. 若使用 SharedPreferences,盡量放到非同步線程;
  3. 減少布局層級,生命週期回呼中也盡量避免耗時工作。

啟動時的黑屏/白屏

原因

其實這是正常現象。布局還沒載入完成時,window 視窗背景就已經先顯示出來了——你看到的黑屏或白屏,就是那個 window 背景。

示例:

解法

透過 Style 處理,有兩種思路。

(1)設定背景圖主題

讓啟動時先顯示一張背景圖,避免空白:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:screenOrientation">portrait</item> <item name="android:windowBackground">>@mipmap/splash</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> </style>

(2)設定透明主題

把視窗背景設為透明,啟動過程不會黑屏,而是等到界面初始化完成後一次顯示出來:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:screenOrientation">portrait</item> </style>

兩者的取捨:

  • 背景圖方案看起來啟動很快,先出現圖片再刷新其他控件,但會有刷新不同步的割裂感。
  • 透明主題方案反而給人「啟動較慢」的觀感,不過界面一次刷出,視覺上是同步的。

(3)在 AndroidManifest.xml 中套用

<application android:name=".App" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true"> <activity android:name=".MainActivity" android:theme="@style/AppTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> //...... </application>

處理之後的效果:

常見的内建主題

android:theme="@android:style/Theme.Dialog" //Activity顯示為對話框模式 android:theme="@android:style/Theme.NoTitleBar" //不顯示應用程序標題欄 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" //不顯示應用程序標題欄,並全屏 android:theme="Theme.Light " //背景為白色 android:theme="Theme.Light.NoTitleBar" //白色背景並無標題欄 android:theme="Theme.Light.NoTitleBar.Fullscreen" //白色背景,無標題欄,全屏 android:theme="Theme.Black" //背景黑色 android:theme="Theme.Black.NoTitleBar" //黑色背景並無標題欄 android:theme="Theme.Black.NoTitleBar.Fullscreen" //黑色背景,無標題欄,全屏 android:theme="Theme.Wallpaper" //用系統桌面為應用程序背景 android:theme="Theme.Wallpaper.NoTitleBar" //用系統桌面為應用程序背景,且無標題欄 android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" //用系統桌面為應用程序背景,無標題欄,全屏 android:theme="Theme.Translucent" //透明背景 android:theme="Theme.Translucent.NoTitleBar" //透明背景並無標題 android:theme="Theme.Translucent.NoTitleBar.Fullscreen" //透明背景並無標題,全屏 android:theme="Theme.Panel " //面板風格顯示 android:theme="Theme.Light.Panel" //平板風格顯示

干我們這行,什麼時候懈怠,就意味著長進的停止;長進的停止,就意味著被淘汰。只能往前沖,直到鳳凰涅槃的那一天。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved