Android 启动白屏,跳转黑屏以及冷启动优化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BigBoySunshine/article/details/82971816

一,白屏

现象:启动app,白屏一段时间后才出现欢迎页

解决:

1,添加style

<style name="AppTheme.Launcher">
    <item name="android:windowDisablePreview">true</item>
</style>

或者

<style name="AppTheme.Launcher">
    <item name="android:windowBackground">@mipmap/welcome</item>
</style>

其中@mipmap/welcome是一个欢迎页图片。相比之下第二种的更好一点,可以达到秒启动。第一种会有短暂的卡顿,然后显示启动页。

2,在Manifest中设置给WelcomeActivity

<activity
	android:name=".main.login.WelcomeActivity"
	android:launchMode="singleTask"
	android:screenOrientation="portrait"
	android:theme="@style/AppTheme.Launcher">
	<intent-filter>
		<action android:name="android.intent.action.MAIN" />

		<category android:name="android.intent.category.LAUNCHER" />
	</intent-filter>
</activity>

3,这样改变了整个app的主题,我们需要在WelcomeActivity中把我们本来的主题设置回去

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 我们本来的style
        setTheme(R.style.AppTheme_NoActionBar)
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(R.layout.activity_welcome)
    }

这样解决了白屏的问题。

二,黑屏

现象:由一个界面跳转到另一个界面时出现黑屏,然后才显示后一个界面。

分析:一般都是因为给AppTheme添加了windowIsTranslucent,这样过渡时前一个Acitivity被finish掉了,而后一个还没显示出来。就会出现黑屏。

解决:把AppTheme的windowIsTranslucent属性去掉。

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
	<item name="colorPrimary">@color/colorPrimaryDark</item>
	<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
	<item name="colorAccent">@color/colorAccent</item>

	<!--<item name="android:windowIsTranslucent">true</item>-->
</style>

三,启动时间优化

我们通常需要在application中做一下三方库的初始化。这些操作都会影响我们的启动速度。

此时我们可以把这些操作放到子线程中完成。如:

new Thread(new Runnable() {
    @Override
    public void run() {
       init();
    }
}).start();

只是举个栗子,不要内存泄漏了。

四,检测启动时间

可以通过adb命令来测试冷启动和热启动的耗时,

adb shell am start -W [包名]/[包名.类名]

如。

猜你喜欢

转载自blog.csdn.net/BigBoySunshine/article/details/82971816