SoFunction
Updated on 2025-03-01

Android solution to prevent APP startup from flashing black screen (Theme and Style)

A few days ago, Boss reported that the machine would flash black every time it started the program, and this customer would not accept it. There is no way, I can only think about how to solve it, and finally found the following method. The main reason for flashing the black screen is that when we start the Activity, we need to run onCreate and onResume before the interface will be displayed. That is to say, some data needs to be processed before it will be displayed. According to this idea, can I avoid black screen by minimizing the initialization work? The truth is that even if you do nothing onCreate, you will still flash black because it takes a certain amount of time to initially resolve the interface. Here is the solution:
1. Customize theme

Copy the codeThe code is as follows:

Set background image Theme
<style name="" parent="android:Theme"> 
    <item name="android:windowBackground">@drawable/ipod_bg</item> 
    <item name="android:windowNoTitle">true</item> 
</style>
//2. Set transparent Theme
<style name="" parent="android:Theme"> 
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item> 
</style>

I have defined two types of themes above. The first type of theme is to set a background image. When the program starts, first display this background image to avoid black screen. The second Theme is to set the style to transparent. After the program starts, the screen will not be black, but will be transparent. It will not be displayed at one time until the interface is initialized.Here are the advantages and disadvantages of the two methods:
•Theme1 program starts quickly, the interface first displays the background image, and then refreshes other interface controls. Refreshing the feeling of out-of-sync.
•Theme2 gives people the feeling of slow start of the program, and the interface is flashed at one time and refreshed and synchronized.
 
2. Modify
In order for the above Theme to take effect, we need to set up some Activity Themes
Copy the codeThe code is as follows:

<application
    android:allowBackup="true"
    android:icon="@drawable/ipod_icon"
    android:label="@string/app_name"
    android:launchMode="singleTask">
<!-- iPod main interface -->
<activity
    android:name=""
<!-- Use the style defined above mythou-->
    android:theme="@style/"
    android:label="@string/app_name" >
    <intent-filter>
    <action android:name="" />
    <category android:name="" />
    </intent-filter>
</activity>
//......
</application>

•You can add the above custom styles to the Activity. In addition, it is also possible to add it in Application, and it is a global effect.
•Place theme in /res/values/. If you don't have this file, just add one by yourself.
•If there are multiple Activity switches, there may also be a brief black screen problem in the middle. The reason is that when the Activity starts, you need to initialize the loading data. If you want to avoid this, you can add the above style to the Activity you switched.
• Both of the above styles can avoid black screen. You can actually test your program to choose an effect.
•This only avoids the black screen, but if your program initialization is slow, it will still give people the feeling of slow program startup. The program initialization process needs to be optimized by itself.
 
3. Detailed explanation of Theme attributes
Copy the codeThe code is as follows:

android:theme="@android:style/" //Activity is displayed as dialog box mode
android:theme="@android:style/" //Do not display the application title bar
android:theme="@android:style/" //Do not display the application title bar and full screen
android:theme=" " //The background is white
android:theme="" //There is no title bar on the white background
android:theme="" //White background, untitled bar, full screen
android:theme="" //Black background
android:theme="" //The black background has no title bar
android:theme="" //Black background, untitled bar, full screen
android:theme="" //Use the system desktop as the application background
android:theme="" //Use the system desktop as the application background and there is no title bar
android:theme="" //Use the system desktop as the application background, without title bar, full screen
android:theme="" //Transparent background
android:theme="" //Transparent background without title
android:theme="" //Transparent background without title, full screen
android:theme=" " //Panel style display
android:theme="" // Tablet style display

4. Theme and Style
In addition to Theme, there are Styles in Android. For example, below is a Style that configures workspace in Launcher.
Copy the codeThe code is as follows:

 <style name="WorkspaceIcon">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_gravity">center</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:singleLine">true</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#FFF</item>
        <item name="android:shadowRadius">2.0</item>
        <item name="android:shadowColor">#B0000000</item>
    </style>

Style can be understood as a set of attributes, which is convenient for different View settings. When we use Style in View, it is the same application method as using Theme. So what is the difference between Style and Theme?

The difference between the two is listed below:
• Styles are used on separate Views, such as: Button, TextView, etc.

•The topic is used in the entire application or an activity through the <application> and <activity> in the topic, and the topic has a global impact on the entire application or an activity.

•If an application uses a theme and the view under the application also uses styles, then when the theme and the style attributes conflict, the style priority is higher than the theme.

The above is to solve the problem of starting a black screen with the program through Theme, and explain the Theme and Style. Through the Theme configuration, you can actually make a welcome page. However, we all hope that the faster the program starts, the better, so we still need to optimize our programs more.