欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android App應用啟動分析與優(yōu)化

 更新時間:2016年07月21日 12:03:01   投稿:lijiao  
這篇文章主要針對Android App應用啟動的分析與優(yōu)化為大家進行詳細介紹,感興趣的小伙伴們可以參考一下

app的啟動方式:
 1.)冷啟
 當啟動應用時,后臺沒有該應用的進程,這時系統(tǒng)會重新創(chuàng)建一個新的進程分配給該應用,這個啟動方式就是冷啟動。冷啟動因為系統(tǒng)會重新創(chuàng)建一個新的進程分配給它,所以會先創(chuàng)建和初始化Application類,再創(chuàng)建和初始化MainActivity類(包括一系列的測量、布局、繪制),最后顯示在界面上。
 2.)熱啟動 
當啟動應用時,后臺已有該應用的進程(例:按back鍵、home鍵,應用雖然會退出,但是該應用的進程是依然會保留在后臺,可進入任務列表查看),所以在已有進程的情況下,這種啟動會從已有的進程中來啟動應用,這個方式叫熱啟動。熱啟動因為會從已有的進程中來啟動,所以熱啟動就不會走Application這步了,而是直接走MainActivity(包括一系列的測量、布局、繪制),所以熱啟動的過程只需要創(chuàng)建和初始化一個MainActivity就行了,而不必創(chuàng)建和初始化Application,因為一個應用從新進程的創(chuàng)建到進程的銷毀,Application只會初始化一次。 

app的啟動流程: 
通過上面的兩種啟動方式可以看出app啟動流程為: 
Application的構造器方法——>attachBaseContext()——>onCreate()——>Activity的構造方法——>onCreate()——>配置主題中背景等屬性——>onStart()——>onResume()——>測量布局繪制顯示在界面上

app的啟動優(yōu)化:
基于上面的啟動流程我們盡量做到如下幾點
 1.Application的創(chuàng)建過程中盡量少的進行耗時操作
 2.如果用到SharePreference,盡量在異步線程中操作
 3.減少布局的層次,并且生命周期回調(diào)的方法中盡量減少耗時的操作

app啟動遇見黑屏或者白屏問題
1.)產(chǎn)生原因 
其實顯示黑屏或者白屏實屬正常,這是因為還沒加載到布局文件,就已經(jīng)顯示了window窗口背景,黑屏白屏就是window窗口背景。
 示例:

 

2.)解決辦法
通過設置設置Style 
(1)設置背景圖Theme 
通過設置一張背景圖。 當程序啟動時,首先顯示這張背景圖,避免出現(xiàn)黑屏

<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" //用系統(tǒng)桌面為應用程序背景

android:theme="Theme.Wallpaper.NoTitleBar" //用系統(tǒng)桌面為應用程序背景,且無標題欄

android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" //用系統(tǒng)桌面為應用程序背景,無標題欄,全屏

android:theme="Theme.Translucent" //透明背景

android:theme="Theme.Translucent.NoTitleBar" //透明背景并無標題

android:theme="Theme.Translucent.NoTitleBar.Fullscreen" //透明背景并無標題,全屏

android:theme="Theme.Panel " //面板風格顯示

android:theme="Theme.Light.Panel" //平板風格顯示
干我們這行,啥時候懈怠,就意味著長進的停止,長進的停止就意味著被淘汰,只能往前沖,直到鳳凰涅槃的一天!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論