【轉載】Android App應用啟動分析與最佳化

來源:互聯網
上載者:User

標籤:

前言:

昨晚新版本終於發布了,但是還是記得有測試反饋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的啟動最佳化:

基於上面的啟動流程我們盡量做到如下幾點

  1. Application的建立過程中盡量少的進行耗時操作
  2. 如果用到SharePreference,盡量在非同步線程中操作
  3. 減少布局的層次,並且生命週期回調的方法中盡量減少耗時的操作
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應用啟動分析與最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.