Three solutions for black screen white screen when Android launches app

Source: Internet
Author: User

You will be very strange, why some apps start, there will be a moment of black screen or white screen to enter the activity interface display, but some apps are not like QQ mobile phone, indeed here to do the processing. Here first understand why this phenomenon, in fact, is very simple, resume a simple example can be understood.

  In fact, the black screen or white screen here is not abnormal, but has not yet loaded into the layout file, has shown the window Windows background, black screen white screen is the window background. The code below, you can write a small demo to understand.   @Overrideprotected void OnCreate (Bundle savedinstancestate) {    super.oncreate (savedinstancestate);    //Note: Add 3 seconds to sleep to make sure the black screen is noticeable for a while, and remove this 3 seconds of sleep     try {        Thread.Sleep (3000) in the project application;     catch (interruptedexception e) {        e.printstacktrace ();   }  &nbs P Before this, the black screen or the white screen is the window background color, is the windows background, not yet the layout of the interface, to perform setcontentview after the layout     Setcontentview ( R.layout.activity_launcher);} Where does the window background provide? In the supply theme, the following is a white background, that is, the start-up white screen a moment of color settings.   <!--Application Theme. --><style name= "Apptheme" parent= "Appbasetheme" >    <item name= "Android:windownotitle" > true</item>    <item name= "Android:windowbackground" > @color/white</item>    <!--all customizations that is not specific to a particular api-level can go. --></sTyle> so, in the theme setting Windowbackground can solve the startup white screen black screen for a while, the following provides three solutions:  one, provide. png background map   provide a background map is a solution, but to adapt to a variety of screens, Many pictures are available. Unless the picture is very complex and can only be used in the background map, use this method, otherwise the individual does not recommend.   B. 9.png (ninepatch) Background image   If the picture is not very complex, can be made into ninepatch pictures, then directly make Ninepatch pictures, provide a card can be adapted to any mobile phone, why not.   using layout-list to make background images   If you can use this method, we recommend using this layout-list to make background images. The first 2 kinds of images used to occupy memory ah, using layout-list compared to save memory, make the app will not say because the volume of the picture becomes larger.    the code below.  launchactivity for the start-up interface after 3 seconds to jump to the main page mainactivity, in order to achieve a black screen white screen more obvious effect, before Setcontentview thread sleep 3 seconds.  public class Launcheractivity extends Activity {    public final int msg_finish_launcheractivity = 500;&NB Sp        public Handler Mhandler = new Handler () {        public void Handlemessage (M Essage msg) {            switch (msg.what) {            Case M sg_finish_launcheractivity:               //jump to Mainactivity, and end the current launcheractivity      &NBSP         Intent Intent = new Intent (launcheractivity.this, Mainactivity.class);      &NB Sp         startactivity (intent);                finish ();                break;             default:  &nbs P             break;           }       };& nbsp  };     @Override     protected void onCreate (Bundle savedinstancestate) {    &NB Sp   Super.oncreate (savedinstancestate);       //Do not display the system's title bar to ensure Windowbackground and interface Activity_ The size of main, shown on the screen will not be misplaced (remove this line to try to know the effect)         GetWindow (). SetFlags ( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);                //Note: Add 3-second sleep to ensure a black screen for a whileObviously, in the project application to get rid of this 3 seconds sleep         Try {            Thread.Sleep (; ) &nbsp ;     catch (interruptedexception e) {            e.printstacktrace ();  &nbsp ;    }                 Setcontentview (r.layout.activity_launcher);                 //stay 3 seconds to send a message, jump to mainactivity        Mhan Dler.sendemptymessagedelayed (msg_finish_launcheractivity, up);   } }activity_ Launcher.xml layout file, it is very simple to remember that the linearlayout used in the background is layout_list_start_pic, with the theme theme use the same background, thus eliminating the effect of the background is not the same. The benefits and effects of doing this are not known until you try it yourself.   <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"     xmlns:tools= "Http://schemas.android.com/tools"     android:layout_width= "fill_parent"     Android:layout_ height= "Fill_parent"     android:background= "@drawable/layout_liSt_start_pic ">     <TextView        android:layout_width=" Wrap_content "         android:layout_height= "wrap_content"         android:textcolor= "#ffffff "        android:text=" @string/hello_world "/> </linearlayout> Androidmanifest.xml, note here that the theme used by application is Apptheme, and Launcheractivity uses the theme Startapptheme. The effect of this is that as long as launcheractivity uses the Startapptheme theme, the other activity is apptheme theme.   <?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/ Apk/res/android "    package=" com.example.launcheractivity "    android:versioncode=" 1 "    Android:versionname= "1.0" >     <uses-sdk        Android:minsdkversion = "8"         android:targetsdkversion= "/>     <application   " nbsp   android:allowbackup= "true"     &NBSP   android:icon= "@drawable/ic_launcher"         android:label= "@string/app_name"         android:theme= "@style/apptheme" >        <activity        &NB Sp   Android:name= ". Launcheractivity "            android:label=" @string/app_name "        & nbsp   Android:theme= "@style/startapptheme" >            <intent-filter>  & nbsp             <action android:name= "Android.intent.action.MAIN"/>                 <category android:name= "Android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    &NBSP ;            <activity android:name= ". Mainactivity "></activity>    &LT;/APplication> </manifest>styles.xml,2 Theme Settings   <resources xmlns:android= "/http Schemas.android.com/apk/res/android >     <!--        Base application theme, dependent on API level. This theme are replaced        by Appbasetheme from Res/values-vxx/styles.xml on newer devices.  & nbsp -->    <style name= "Appbasetheme" parent= "Android:Theme.Light.NoTitleBar" >        <!--            Theme customizations available in newer API levels can go in            Res/values-vxx/styles.xml, while customizations related to            backward-compatibility can go here.       -->    &LT;/STYLE&GT;&NBSP;&NBS P   <!--application theme. -->    <style name= "Apptheme" parent= "Appbasetheme" >        <item name= "Android:windownotitle" >true</item>        <item name= "Android:windowbackground" > @color/white</item>        <!--all customizations that is not specific to a particular Api-level can go here. -->    </style>    <style name= "Startapptheme" parent= "Appbasetheme" >        <item name= "Android:windownotitle" >true</item>        <item name= " Android:windowbackground "> @drawable/layout_list_start_pic</item>        <!--all Customizations that is not specific to a particular api-level can go here. -->    </style></resources>layout_list_start_pic.xml Start Page Use this as a background image   <?xml Version= "1.0" encoding= "Utf-8"? ><layer-list xmlns:android= "Http://schemas.android.com/apk/res/android" >     <!--set the entire screen background to white-->    <item >        <color android:color= "@color/white"/>    </item>     <!--Middle logo-->    <item >        <bitmap            android:gravity= "Cente R "            android:src=" @drawable/ic_launcher "/>    </item>  & nbsp <!--bottom chart-->    <item android:bottom= "10DP" >        <bitmap    &N Bsp       android:gravity= "bottom|center_horizontal"             android:src= "@ Drawable/copyright "/>    </item> </layer-list> 

A companion tour, a free dating site: www.jieberu.com

Push family, free tickets, scenic spots: www.tuituizu.com

Three solutions for black screen white screen when Android launches app

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.