Android to avoid app startup Flash black screen solution (theme and style) _android

Source: Internet
Author: User
Tags set background

A few days ago boss on the response that the machine every start program will flash a black screen, this customer does not accept. No way, can only think how to solve, finally found the following method. The main reason for flashing black screen is that when we start the activity, we need to run the OnCreate and Onresume to show the interface. This means that some data needs to be processed before it is displayed. In this way, is it possible for me to avoid the black screen by minimizing the initialization work? The truth is, even if you do nothing at all, you will still oncreate the black screen, because it takes a while to initialize the parsing interface. Here is the solution:
1, custom theme

Copy Code code as follows:

Set Background image theme
<style name= "Theme.appstartload" parent= "Android:theme" >
<item name= "Android:windowbackground" > @drawable/ipod_bg</item>
<item name= "Android:windownotitle" >true</item>
</style>
2. Set Transparent theme
<style name= "theme.appstartloadtranslucent" parent= "Android:theme" >
<item name= "Android:windowistranslucent" >true</item>
<item name= "Android:windownotitle" >true</item>
</style>

I've defined two kinds of theme, the first theme is to set up a background picture. When the program starts, display this background image first, avoid the black screen. The second type of theme is to set the style to transparent, the program will not be launched after the black screen but the entire transparent, wait until the interface is initialized to display only once. Here are the pros and cons of two ways:
the theme1 program starts fast, the interface first displays the background image, and then refreshes the other interface controls. Give people a refreshing feeling of not being synchronized.
Theme2 gives the program start slow feeling, the interface brushes out once, refreshes the synchronization.

2. Modify Androidmanifest.xml
In order for the above theme to take effect, we need to set up some activity theme
Copy Code code 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= "Com.apical.apicalipod.IPodMainActivity"
<!--use the style defined above mythou-->
Android:theme= "@style/theme.appstartload"
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
//......
</application>

• The above custom style can be added to the activity. In addition, it is also possible to increase the application inside, and it is the global effect.
• Custom theme placed inside the/res/values/styles.xml. If you don't have this file, add one yourself.
• If there is more than one activity switch, there may be a short black screen problem in the middle. The reason is that the activity needs to initialize the loading data when it starts, and if you want to avoid this, add the above style to the activity you're switching.
• The above two styles can be avoided black screen. You can actually test your program to choose an effect.
This is just a black screen to avoid, but if you start the program initialization slow, or it will give the program start slow feeling. It is necessary to optimize the initialization process of the program.
 
3, Theme attribute detailed
Copy Code code as follows:

Android:theme= "@android: Style/theme.dialog"//activity displayed as dialog box mode
Android:theme= "@android: Style/theme.notitlebar"//Do not display the application title bar
Android:theme= "@android: Style/theme.notitlebar.fullscreen"//Do not display the application title bar and full screen
Android:theme= "Theme.light"//Background is white
Android:theme= "Theme.Light.NoTitleBar"//white background with no title bar
Android:theme= "Theme.Light.NoTitleBar.Fullscreen"//white background, no title bar, full screen
Android:theme= "Theme.black"//Background Black
Android:theme= "Theme.Black.NoTitleBar"//black background with no title bar
Android:theme= "Theme.Black.NoTitleBar.Fullscreen"//black background, no title bar, full screen
Android:theme= "Theme.wallpaper"//with System desktop for application background
Android:theme= "Theme.Wallpaper.NoTitleBar"//with System desktop for application background with no title bar
Android:theme= "Theme.Wallpaper.NoTitleBar.Fullscreen"//with System desktop for application background, no title bar, full screen
Android:theme= "theme.translucent"//Transparent Background
Android:theme= "Theme.Translucent.NoTitleBar"//Transparent background with no title
Android:theme= "Theme.Translucent.NoTitleBar.Fullscreen"//Transparent background with no title, full screen
Android:theme= "Theme.panel"//panel style display
Android:theme= "Theme.Light.Panel"//Flat style display

4, Theme and style
Android has style in addition to theme, for example, the following is a style for launcher configuration workspace
Copy Code code 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 that are easy to use for different view settings, and we use style in view to use the same method as theme. So what's the difference between style and theme?

The difference between the two is listed below:
• Style used in separate view, such as Button, TextView, etc.

• Themes are used throughout the application or an activity through the <application> and <activity> in Androidmanifest.xml, and themes have a global impact on the entire application or on an activity.

• If an application uses a theme, and the view that is applied also uses a style, the style takes precedence over the theme when the theme conflicts with the style attribute.

Above is through the theme solution to start flashing black screen problem, and explained the theme and style, through the theme configuration, in fact, can also be a welcome page. But we all want the program to start faster and better, so we still need to optimize our program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.