[Android] solve start APP flash black / black and white

Foreword

Problem: there will be a period of time flash white or black screen in the startup process before entering the App layout interface.

First look

Cold start

Has not been loaded before the App VM system to a virtual machine, and no cache data this App and the App without the background thread, start the App called: Let start

Hot Start

After the App starts, press the back key to exit the program at this time App thread also caches in the background, after the start App Application as it has been cached, so direct access to the Activity. Known as: hot start

The source of the problem

Application of Theme or Activity such as using the following Theme:

<style name="AppTheme" parent="@android:style/Theme.Light.NoTitleBar">
    <!-- 令启动App时,出现闪白屏-->
</style>
<style name="AppTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <!-- 令启动App时,出现闪黑屏-->
</style>

The reason: when you start App, Activity inonCreate()-->setContentView()并不会马上加载布局,而是先初始化绘制Application window窗体,这个时候布局还没加载,使用的是默认背景色,其次才执行setContentView()。所以就出现了闪白或黑屏的现象。

solution

This program is the main entry to start SplashActivity start page, a new thread in the delay time to start MainActivity onCreate () method.

Core code system background properties:

<item name="android:windowBackground">@drawable/image</item>

Defined style: 

<style name="SplashTheme" parent="AppTheme">
    <!--设置背景图片-->
    <item name="android:windowBackground">@drawable/image</item>
</style>

References in AndroidManifest

<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>

In the code

new Thread(new Runnable() {
			
    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        }
        catch (InterruptedException e)
        {}
        startActivity(new Intent(MainActivity.this, A.class));
        finish();
    }
}).start();

 

Published 15 original articles · won praise 16 · views 4160

Guess you like

Origin blog.csdn.net/qq_42470947/article/details/104073212