標籤:
前言:
昨晚新版本終於發布了,但是還是記得有測試反饋app啟動好長時間也沒進入app首頁,所以今天準備加個班總結一下App啟動那些事!
app的啟動方式:1.)冷啟動
當啟動應用時,後台沒有該應用的進程,這時系統會重新建立一個新的進程分配給該應用,這個啟動方式就是冷啟動。冷啟動因為系統會重新建立一個新的進程分配給它,所以會先建立和初始化Application類,再建立和初始化MainActivity類(包括一系列的測量、布局、繪製),最後顯示在介面上。
2.)暖開機
當啟動應用時,後台已有該應用的進程(例:按back鍵、home鍵,應用雖然會退出,但是該應用的進程是依然會保留在後台,可進入工作清單查看),所以在已有進程的情況下,這種啟動會從已有的進程中來啟動應用,這個方式叫暖開機。暖開機因為會從已有的進程中來啟動,所以暖開機就不會走Application這步了,而是直接走MainActivity(包括一系列的測量、布局、繪製),所以暖開機的過程只需要建立和初始化一個MainActivity就行了,而不必建立和初始化Application,因為一個應用從新進程的建立到進程的銷毀,Application只會初始化一次。
app的啟動流程:
通過上面的兩種啟動方式可以看出app啟動流程為:
Application的構造器方法——>attachBaseContext()——>onCreate()——>Activity的構造方法——>onCreate()——>配置主題中背景等屬性——>onStart()——>onResume()——>測量布局繪製顯示在介面上
app的啟動最佳化:
基於上面的啟動流程我們盡量做到如下幾點
Application的建立過程中盡量少的進行耗時操作
- 如果用到
SharePreference,盡量在非同步線程中操作
- 減少布局的層次,並且生命週期回調的方法中盡量減少耗時的操作
app啟動遇見黑屏或者白屏問題1.)產生原因
其實顯示黑屏或者白屏實屬正常,這是因為還沒載入到布局檔案,就已經顯示了window視窗背景,黑屏白屏就是window視窗背景。
樣本:
2.)解決辦法
通過設定設定Style
(1)設定背景圖Theme
通過設定一張背景圖。 當程式啟動時,首先顯示這張背景圖,避免出現黑屏
<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)設定透明Theme
通過把樣式設定為透明,程式啟動後不會黑屏而是整個透明了,等到介面初始化完才一次性顯示出來
<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>
兩者對比:
- Theme1 程式啟動快,介面先顯示背景圖,然後再重新整理其他介面控制項。給人重新整理不同步感覺。
- Theme2 給人程式啟動慢感覺,介面一次性刷出來,重新整理同步。
(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>
解決後樣本:
3.)常見的Theme主題
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 App應用啟動分析與最佳化