Android-APP启动时黑屏-白屏

Android应用启动时常出现黑屏或白屏现象,主要是由于界面绘制前的任务集中化导致。解决方法包括使用Theme调整,如采用透明主题但可能造成视觉停顿。正确实现启动画面应在AndroidManifest.xml中设置SplashTheme,并在SplashActivity避免setContentView(),确保快速显示。参考文献提供了更多详细解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于用户来讲,启动画面是浪费时间的,作为开发者应该尽量避免过长的启动画面,但是安卓应用在启动时很耗时,尤其是冷启动,这个延时是难以避免的,即使只有一个Helloworld应用也有可能出现延时。
这里写图片描述
究其原因,主要因素是任务在界面绘制前过于集中化。应用启动过程从用户点击launcher图标到看到第一帧这个过程中,主要会经过以下这些过程:

main()->Application:attachBaseContext()->onCreate()-Activity:onCreate()->onStart()->onPostCreate()-onResume()->onPostResume()

要实现一个启动画面,避免黑/白屏出现,主要由Theme着手,有人用设置透明主题的方式解决这一问题,会出现点击图标后停顿一下再打开应用,其实跟黑/白屏并无区别,更改了颜色而已,应用其实已经启动了。

为了实现点击图标立即显示不能用inflate一个layout的方式,创建一个splash.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@color/colorPrimary"/>
        <item>
            <bitmap
                android:gravity="center"
                android:src="@mipmap/ic_launcher"/>
        </item>
    </layer-list>

这里设置了背景颜色和一个图片,然后将整个文件设置为SplashTheme的背景:

    <resources>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        </style>

        <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
            <item name="android:windowBackground">@drawable/bg_splash</item>
        </style>
    </resources>

然后在AndroidManifest.xml中将SplashTheme设置到SplashActivity:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="edu.jnu.splashdemo">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
            </activity>
            <activity android:name=".SplashActivity"
                android:theme="@style/SplashTheme">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

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

    </manifest>

最后,在SplashActivity中设置启动跳转:

    public class SplashActivity extends AppCompatActivity {
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

onCreate()中不要设置setContentView()。
这里写图片描述

参考文献:

[1] https://www.bignerdranch.com/blog/splash-screens-the-right-way/

[2] http://blog.youkuaiyun.com/yanzhenjie1003/article/details/52201896

[3] http://www.jianshu.com/p/03c0fd3fc245

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值