现象:
打开一个APP时,会有几秒的白屏或者是黑屏出现
原因:
当我们在启动一个应用时,系统会检查是否已经存在这样一个进程,如果没有,Android系统会创建出一个新的进程分配给该应用,之后会依次创建和初始化Application类、然后启动SplashActivity类的。而显示白黑屏的问题就是在这段时间内产生的。
系统会在绘制页面加载布局之前 ,首先会初始化窗口(Window),而在进行这步操作时,系统会根据我们设置的Theme来指定它的Theme主题颜色。Window布局的顶层是DecorView,StartingWindow显示一个空的DecorView,进而我们在Style中的设置就决定了显示的是白屏还是黑屏。
处理:
在应用要启动的第一个页面(即应用的启动页)中设置一个单独的Theme主题。
<activity android:name=".MainActivity" android:theme="@style/AppStartTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
上面代码中的
android:theme="@style/AppStartTheme"
指定了你的启动页的主题。
<style name="AppStartTheme" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@mipmap/welcome</item>
</style>
或者
<style name="AppStartTheme" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@color/check_orange</item>
</style>