1、产生原因
冷启动(cold start),暖启动(warm start),热启动(lukewarm start)。
- 冷启动:是指进程从无到有的过程。因为要进行页面初始化,所以相对其他两个启动方式,消耗的时间是相对比较多的。
- 热启动:是指之前的进程还在,在之前进程的基础上创建 Activity 的过程。
我们遇到的白屏黑屏问题就是在app的冷启动时期产生的,一般就是app从后台移除再点击进入的时候。因为在app冷启动的时候需要做一些了初始化操作application、LAUNCHER。在一些列初始化的时间里系统为通知用户已进入app会弹出一个预加载页面告诉app已经启动。如果未做任何处理就会出现白屏和黑屏的现象。
2、解决方式
解决方式主要是对启动页Theme的处理
<activity
android:name=".LogoActivity"
android:theme="@style/StartTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
(1)隐藏预加载页面(不推荐)
既然白屏的原因就是app预加载页面弹出造成的,我们可以把它隐藏掉。
<style name="StartTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:windowDisablePreview">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowBackground">@android:color/white</item>
</style>
把windowDisablePreview设置成true,这样就不会出现白屏和黑屏的问题然而会使我们点击app的时候会短暂的停留在手机屏幕一段时间然后直接进入LAUNCHER_ACTIVITY,造成一种手机卡顿的现象,在性能差的手机上更明显。(360手机助手就是这种现象)
(2)为启动页Theme设置一个透明背景(不推荐)
<style name="StartTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowBackground">@android:color/white</item>
</style>
把windowIsTranslucent设置成true,类似于把预加载页面隐藏了,最终效果类似于隐藏预加载页面。
(3)给启动页Theme设置一个和启动页一样的背景图片
<style name="StartTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@drawable/layer_launcher</item>
<item name="android:windowFullscreen">true</item>
</style>
为解决不同屏幕手机图片被拉伸的问题(华为手机虚拟键盘),我们创建一个layer-list drawable文件。这也解决了启动页图片跳跃的问题。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
<item>
<bitmap
android:gravity="center"
android:src="@drawable/background" />
</item>
</layer-list>
同时注释掉// setContentView(R.layout.activity_logo);,这样就保证了预加载页面和启动页一致。
3、特殊情况
做完上面的操作基本上已经解决了闪屏的问题。当在观察华为手机时,再从预加载页面切到LAUNCHER_ACTIVITY时会闪一下(淘宝启动页偶尔就会出现)。原因可能是在LAUNCHER_ACTIVITY的onCreate中执行了一些反序列化和网络请求等耗时操作。我的解决方式是创建一个Handle延迟一秒执行初始化操作。
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSharedPreferences = getSharedPreferences("phone", Context.MODE_PRIVATE);
//友盟
MobclickAgent.setDebugMode(true);
MobclickAgent.enableEncrypt(true);
mRequestQueue = Volley.newRequestQueue(LogoActivity.this);
getNewVersion();
}
},1000);
到此问题基本上就解决完了,要是有新的问题可以提一下。