Android - Prevent white screen at startup

2019-01-10 00:14发布

As we all know, many Android apps display a white screen very briefly before their first Activity comes into focus. This problem is observed in the following cases:

  • Android apps that extend the global Application class and perform major initializations therein. The Application object is always created before the first Activity (a fact that can be observed in the debugger), so this makes sense. This is the cause of the delay in my case.

  • Android apps that display the default preview window before the splash screen.

Setting android:windowDisablePreview = "true" obviously does not work here. Nor can I set the parent theme of the splash screen to Theme.Holo.NoActionBar as described here, because [unfortunately] my splash screen makes use of an ActionBar.

Meanwhile, apps that do not extend the Application class do not show the white screen at startup.

The thing is, ideally the initializations performed in the Application object need to occur before the first Activity is shown. So my question is, how can I perform these initializations on app startup without using an Application object? Possibly using a Thread or Service, I suppose?

This is an interesting problem to think about. I can't bypass it the usual way (by setting the NoActionBar theme), as tragically my Splash screen actually has an ActionBar due to some unrelated reasons.

Note:

I have already referred to the following questions:

References:

14条回答
对你真心纯属浪费
2楼-- · 2019-01-10 01:14

Just write the item in values/styles.xml:

<item name="android:windowBackground">@android:color/black</item>

For example, in the AppTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>

    <item name="android:windowBackground">@android:color/black</item>

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
查看更多
虎瘦雄心在
3楼-- · 2019-01-10 01:19
Style :- 
<style name="SplashViewTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

In Manifest :- 
<activity android:name=".SplashActivity"
        android:theme="@style/SplashViewTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
查看更多
登录 后发表回答