Android 冷啟動(dòng)與跨進(jìn)程啟動(dòng)黑屏問題

原文鏈接:http://www.cnblogs.com/feidu/p/8057012.html

當(dāng)Android跨進(jìn)程啟動(dòng)Activity時(shí),過程界面會(huì)黑屏(白屏)短暫時(shí)間(幾百毫秒?)。當(dāng)然從桌面Lunacher啟動(dòng)一個(gè)App時(shí)也會(huì)出現(xiàn)相同情況,那是因?yàn)锳pp冷啟動(dòng)也屬于跨進(jìn)程啟動(dòng)Activity。為什么沒會(huì)出現(xiàn)這種情況呢?真正元兇就是Android創(chuàng)建進(jìn)程需要準(zhǔn)備很多資源,它是一個(gè)耗時(shí)的操作。

黑屏(白屏)原因

當(dāng)A進(jìn)程啟動(dòng)B進(jìn)程中的一個(gè)Activity時(shí),Android系統(tǒng)會(huì)先有zygote進(jìn)程創(chuàng)建B進(jìn)程,然后才能啟動(dòng)這個(gè)Activity。但創(chuàng)建進(jìn)程是耗時(shí)的,在創(chuàng)建完成之前,新的Activity界面還沒機(jī)會(huì)展示,如此用戶在跳轉(zhuǎn)新的Activity時(shí)會(huì)短暫沒反應(yīng),這極大的降低用戶體驗(yàn)。

Android團(tuán)隊(duì)避免出現(xiàn)這種尷局面,于是系統(tǒng)根據(jù)你的manifest文件設(shè)置的主題顏色的不同來展示一個(gè)白屏或者黑屏。而這個(gè)黑(白)屏被稱作Preview Window,即預(yù)覽窗口。

解決方案

1.禁用Preview Window

既然Android在創(chuàng)建進(jìn)程啟動(dòng)新Activity時(shí)默認(rèn)設(shè)置了Preview Window,那么我們也可以在主題中禁用該屬性。

style.xml

<style name="APPTheme" parent="@android:style/Theme.Holo.NoActionBar">

<item name="android:windowDisablePreview">true</item>

</style>?

分析:這樣做可以解決部分場(chǎng)景的問題,比如在A進(jìn)程啟動(dòng)B進(jìn)程中的Activity;但是在另外一個(gè)場(chǎng)景就有問題了,在桌面Launcher點(diǎn)擊應(yīng)用出現(xiàn)短暫的假死現(xiàn)象。

2.自定義Preview Window

既然Android可以根據(jù)主題設(shè)置Preview Windo黑屏(白屏),那么我們也可以自定義一個(gè)Preview Window樣式來代替黑(白)屏效果。

style.xml

<style name="APPTheme" parent="@android:style/Theme.Holo.NoActionBar">

<item name="android:windowBackground">@drawable/splash_icon</item>

<item name="android:windowFullscreen">true</item>

<item name="android:windowNoTitle">true</item>

</style>?

分析:該解決方案很適合啟動(dòng)一個(gè)App場(chǎng)景,android:windowBackground屬性設(shè)置Preview Window的背景,市面上大部分App都是使用該屬性設(shè)置啟動(dòng)頁背景。出于節(jié)省內(nèi)存的考慮該背景圖片適合使用效果簡(jiǎn)單的.9圖片。

但是該解決方案不適合在跨進(jìn)程啟動(dòng)Activity場(chǎng)景了。

3.設(shè)置Preview Window透明屬性

我們可以設(shè)置Preview Window 為透明,也可以解決問題

style.xml

true@android:color/transparenttruetrue

分析:該解決方案適合跨進(jìn)程啟動(dòng)Activity場(chǎng)景使用。當(dāng)然這個(gè)解決方案也會(huì)引入其他問題,就是:android:windowIsTranslucent 引起activity切換動(dòng)畫無效解決方案。為了追求極致,不能解決一個(gè)問題引入一個(gè)新問題,該問題的解決方案也有兩種:

代碼動(dòng)態(tài)設(shè)置Activity專場(chǎng)動(dòng)畫

overridePendingTransition(R.anim.anim_right_in,R.anim.anim_left_out);

給Window 設(shè)置動(dòng)畫style

<style name="APPTheme" parent="@android:style/Theme.Holo.NoActionBar">

<item name="android:windowIsTranslucent">true</item>

<item name="android:windowBackground">@android:color/transparent</item>

<item name="android:windowFullscreen">true</item>

<item name="android:windowNoTitle">true</item>

<item name="android:windowAnimationStyle">@styleAnimation.Activity.Translucent.Style/</item>

</style>

<style name="Animation.Activity.Translucent.Style" parent="@android:style/Animation.Translucent">

<item name="android:windowEnterAnimation">@anim/base_slide_right_in</item>

<item name="android:windowExitAnimation">@anim/base_slide_right_out</item>

</style>

自此,跨進(jìn)程啟動(dòng)Activity黑(白)屏的三種方案已給出,讀者可以根據(jù)不同場(chǎng)景使用以上不同解決方案。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容