您的位置:首页 > 移动开发

APP启动出现白屏(黑屏)的问题

2016-10-13 20:40 323 查看
启动的时候出现白屏(黑屏)的原因都是因为启动的线程请求处理数据过大,导致启动第一个页面之前会有一个短暂的闪屏的现象,具体白色、黑色通过当前APP使用的Theme有关。
我解决这个问题的方法是重新开一个Activity并给他指定一个单独的Theme,作为启动欢迎界面,并不在这个Activity中做任何操作:

1、创建欢迎界面Activity

public class SplashActivity extends BaseActivity {
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
application.finish(SplashActivity.this);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

handler.sendEmptyMessageDelayed(0, 1000);
}
}


2、在styles.xml中定义启动类用到的Theme

<!--启动页面样式-->
<style name="AppSplash" parent="@style/AppTheme">
<item name="android:windowBackground">@drawable/splash_layers</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>


3、创建/drawable/splash_layers.xml,在启动Theme中用到

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/window_background" />
</item>
<!-- 底部图表 -->
<item android:bottom="10dp">
<bitmap
android:gravity="bottom|center_horizontal"
android:src="@mipmap/splash" />
</item>
</layer-list>


4、在manifest中注册activity

<application
android:name=".CodeApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity
android:name=".SplashActivity"
android:configChanges="screenSize|orientation"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/AppSplash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".MainActivity"
android:configChanges="screenSize|orientation"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize" />
</application>


按照上面几个步骤就能消除启动白屏的情况。
在做的过程中发现一个有趣的情况,一开始按以上步骤做好了以后,白屏时间确实有所缩短,但是还是会出现几百毫秒的白屏,一直没有找到原因。
后来调试中又慢慢消失了,一直没有找到原因,我想可能是设置好了之后clean一把,估计就没有这个问题了,如有哪位大牛知道,请给我留言赐教。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息