Android 冷启动白屏问题

460 阅读2分钟

前言

冷启动:App第一次启动的时候会加载一个Application进程,首先会创建和初始化Appliation类,然后在加载Activity。
热启动:利用后台已经拥有的进程,从而避免了Application的加载。直接去加载Activity。

解决白屏的方案大致有以下四种

  • 加入动画
  • 使用占位图
  • 使用图片
  • 使用windowDisablePreview属性

这里我就描述一下使用占位图和windowDisablePreview属性

windowDisablePreview属性<style name=[color=rgb(0, 153, 0) !important]"SplashTheme" parent=[color=rgb(0, 153, 0) !important]"AppTheme"> <item name=[color=rgb(0, 153, 0) !important]"android:windowDisablePreview">[color=rgb(0, 153, 0) !important]true</item> </style>

  • 1
  • 2
  • 3

在AndroidManifest中設置主Activity的theme值,在主Activity中还原。

[color=rgb(155, 133, 157) !important]@Override [color=rgb(0, 0, 136) !important]protected [color=rgb(0, 0, 136) !important]void [color=rgb(0, 153, 0) !important]onCreate(@Nullable Bundle savedInstanceState) { setTheme(R.style.AppTheme); getWindow().setBackgroundDrawableResource(R.drawable.main_splash_bg); [color=rgb(0, 0, 136) !important]super.onCreate(savedInstanceState); }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

运行后发现启动App延迟几秒后跳转到主Activity(有点类似掌上英雄联盟,会让用户误以为是自己手机的问题)。

使用占位图 <style name=[color=rgb(0, 153, 0) !important]"SplashTheme" parent=[color=rgb(0, 153, 0) !important]"AppTheme"> <item name=[color=rgb(0, 153, 0) !important]"android:windowBackground">@drawable/shape_launch</item> <item name=[color=rgb(0, 153, 0) !important]"android:windowFullscreen">[color=rgb(0, 153, 0) !important]true</item> </style>

  • 1
  • 2
  • 3
  • 4

[color=rgb(0, 102, 102) !important]<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"> [color=rgb(0, 102, 102) !important]<item android:drawable="@android:color/white"/> [color=rgb(0, 102, 102) !important]<item> [color=rgb(0, 102, 102) !important]<bitmap android:src="@mipmap/main_splash_bg" android:gravity="fill" /> [color=rgb(0, 102, 102) !important]</item>[color=rgb(0, 102, 102) !important]</layer-list>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

使用windowBackground属性来完成其他操作同windowDisablePreview属性的设置。运行后发现明显白屏问题得到有效解决。