What is the reason for white screen on launch of app ? How to avoid it completely?

不问归期 提交于 2020-01-01 12:31:11

问题


I have a simple app in which I am using openssl for some encryption. but I am getting white screen on launch of app for than 5 seconds.

Here is my gradle:

apply plugin: 'com.android.application'

android {
    signingConfigs {
        config {
            keyAlias 'manvish'
            keyPassword 'manvish'
            storeFile file('/home/manvish/Android/debug.keystore')
            storePassword 'manvish'
        }
    }

        compileSdkVersion 25
        buildToolsVersion '26.0.2'
        defaultConfig {
            applicationId "com.example.manvish.bwssb"
            minSdkVersion 19
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            vectorDrawables.useSupportLibrary = true

        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        }



    android {
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
        }


    }

    android {
        defaultConfig {

                ndk {
                    moduleName "myLibrary"
                    ldLibs "log"
                }

        }
        externalNativeBuild {
            ndkBuild {
                path 'src/main/jni/Android.mk'
            }
        }


    }

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:support-vector-drawable:25.3.1'

    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'
    compile files('libs/MorphoSmart_SDK_6.13.2.0-4.1.jar')

    //    compile 'com.google.android.gms:play-services-location:11.0.4'

    //    compile 'com.google.android.gms:play-services-maps:11.0.4'
    compile files('libs/commons-io-2.4.jar')
    compile('org.apache.httpcomponents:httpmime:4.3') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    compile('org.apache.httpcomponents:httpcore:4.4.1') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'

    }

}

Have a look at Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := ssl_static
LOCAL_SRC_FILES := precompiled/libssl.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := crypto_static
LOCAL_SRC_FILES :=precompiled/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := myLibrary
TARGET_PLATFORM := android-3
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_C_INCLUDES = $(LOCAL_PATH)/include
LOCAL_STATIC_LIBRARIES := ssl_static crypto_static
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

I am not getting the reason behind white screen, some of SO answers suggesting to use this in theme:

<item name="android:windowDisablePreview">true</item>

but I don't want to use this. It is not a proper solution because it avoid white screen but still the delay of loading app is same.please help to solve this.


回答1:


What is white screen while starting an application ?

The white screen comes when we start an application, then every application has to be placed in phone memory to work. While application, putting it's working component in the memory, Android OS shows as the white screen or we can say that cold start. That's why if a user with low memory phone opens application, white screen visible for longer time because application is waiting for putting it's component in the memory(i.e. RAM) to work.

How to resolve Cold start ?

Basically, cold starts time is depends on certain parameters. That is,

1) Storage available in user's phone

2) Heavy loading components placed on MainThread of MainActivity (i.e. Launcher Activity)

Now solutions,

As a developer we can't do anything much about first parameter of cold start, because we can't force user to clear memory every time when he or she open's our application. But, we can optimize our application to perform better on each type of devices either it has low or high memory.

Optimizations for reducing cold start time,

Simply, if you are using heavy view components in your application or you are doing heavy network work on MainThread or either any kinda work, which results in blocking or increasing in render time of your LauncherActivity. Then you have to do it in background or simply, render common components first then load full view in background.

Now, about removing white screen

On the first thought, you can't remove white screen. But you can replace it by either use,

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

in your LauncherActivities style.So, if you don't want to AppTheme color then you can set custom color with above method. Or you can replace white screen with your AppTheme primaryColor by placing it in onCreate() method of your activity as,

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);   //it will shows the default color from your apptheme in place of white screen, you can also define your theme in style
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
}



回答2:


That's because cold start of an application. You can't remove that white screen delay but you can utilise that time for displaying branding or color, etc.

There are multiple tricks to replace white screen with branding or background color.

One of them is android:windowBackground where you can declare a xml drawable or simply use any drawable file.

There are couple of links from where you would know more:

  • Use cold start time effectively with a branded launch theme
  • Avoiding cold starts on Android


来源:https://stackoverflow.com/questions/48536680/what-is-the-reason-for-white-screen-on-launch-of-app-how-to-avoid-it-completel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!