編輯:Android 資訊
新版本終於發布了,但測試仍舊回報:App 啟動要等好久才進到首頁。於是今天來把 App 啟動這件事好好梳理一遍。
啟動時背景中沒有該 App 的進程,系統必須重新建立一個進程分配給它。這條路徑會先建立並初始化 Application,接著建立並初始化 MainActivity(包含測量、布局、繪製一連串流程),最後才顯示在畫面上。
啟動時該 App 的進程已經存在——按 BACK 或 HOME 離開後,進程通常仍留在背景,可以到最近任務清單中確認。此時會直接從既有進程啟動,因此不會再走 Application,而是直接進入 MainActivity。
原因很簡單:一個 App 從進程建立到銷毀,Application 只會初始化一次。
綜合兩種方式,冷啟動的順序是:
Application 建構子 → attachBaseContext() → onCreate() → Activity 建構子 → onCreate() → 套用主題背景等屬性 → onStart() → onResume() → 測量布局繪製並顯示
依上面的流程,原則有三條:
其實這是正常現象。布局還沒載入完成時,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" //平板風格顯示
干我們這行,什麼時候懈怠,就意味著長進的停止;長進的停止,就意味著被淘汰。只能往前沖,直到鳳凰涅槃的那一天。
Android 內存優化重點整理:GC、洩漏與內存抖動
Android 應用效能問題中,內存往往是最容易被忽略的一環。良好的內存管理不只是為了避免 OutOfMemoryError,也會直接影響畫面流暢度、GC 頻率與整體穩定
深入剖析 Android View 的測量、佈局與繪製三大流程
Android 中的任何一個佈局、任何一個控件,歸根到底都是直接或間接繼承自 View 的——包括我們日常寫的那些炫酷自定義控件。正因如此,把 View 的工作原理吃透,
Picasso 源碼透視:一個優秀圖片加載庫是如何設計的
圖片加載是 Android 開發的重頭戲,優秀的庫也不少:ImageLoader、Picasso、Glide、Fresco 各有所長。網上關於用法與橫向對比的文章已經很多
Android 四種啟動模式實測驗證:從日志看行為
過去寫啟動模式,多是照著別人的文章抄。近來讀了《Android 開發藝術探索》,才發現自己以往的理解過於粗糙,很多邊界情況根本沒想過。於是決定親手把四種啟動模式逐一驗證一