catalogue

  • 01. A blank screen exists
    • 1.1 Problem Description
    • 1.2 Problem Analysis
  • 02. The solution to the white screen
    • 2.1 Solution Analysis
    • 2.2 The first solution
    • 2.3 The second solution
    • 2.4 Points to Note
  • 03.Application startup speed optimization
  • 04. Launch page mask back button

Good news

  • Summary of blog notes [March 2016 to present], including Java basic and in-depth knowledge points, Android technology blog, Python learning notes, etc., including the summary of bugs encountered in daily development, of course, I also collected a lot of interview questions in my spare time, updated, maintained and corrected for a long time, and continued to improve… Open source files are in Markdown format! Also open source life blog, since 2012, accumulated a total of 50 articles [nearly 300,000 words], reprint please indicate the source, thank you!
  • Link address:Github.com/yangchong21…
  • If you feel good, you can star, thank you! Of course, also welcome to put forward suggestions, everything starts from small, quantitative change causes qualitative change!
  • Multicast map package library: github.com/yangchong21…
  • Lightweight version update popup: github.com/yangchong21…
  • Notification package library: github.com/yangchong21…

01. A blank screen exists

1.1 Problem Description

  • Problem Description
    • The problem of black screen on the startup page of Android App. If there is no special treatment during the startup of Android app, the phenomenon of blank screen will appear for a moment.
    • Even if you load a layout as soon as you launch the screen and do nothing else time-consuming, it seems like you’ll get a momentary blank screen. Note that some areas are also called black screen, depending on the style you set for your app.
    • When clicking on the small icon of the desktop Launcher to launch an App, the program needs to perform some basic initialization operations. For example, many time-consuming operations are performed in the Application or SplashActivity, such as initializing third-party SDKS. When the performance of the phone is poor and the configuration is not high, This is especially true.

1.2 Problem Analysis

  • Why is this a problem
    • When the system starts an APP, the Zygote process will first create a new process to run the APP, but the process creation is time, before the creation is complete, the interface is in a state of suspended animation, so the system will display a white or black screen depending on the theme color of your manifest file. The black (and white) screen is officially called a Preview Window.
    • This is actually caused by the activity’s default theme android:windowBackground being white or black.
    • In summary, the launch sequence is: App start — Preview Window(also known as Preview Window) — start page

02. The solution to the white screen

2.1 Solution Analysis

  • Android’s choice to display a black or white screen varies depending on the theme you set. That is, although your code is not executed, your configuration files are read in advance and used to display the Preview Window interface. So, the entry point for the solution is the manifest file for the entire APP, or more specifically, the theme configuration file.
  • Setting the windowBackground property in the configuration file style to display a background image also has the effect that launching the application feels very fast and asynchronously with the contentView that loads the MainActivity.

2.2 The first solution

  • Solution: Add a background style to the current launch page
    • Set the style style as follows
    <style name="SplashTheme" parent="AppTheme">
        <item name="android:windowBackground">@mipmap/splash</item>
        <item name="android:statusBarColor" tools:ignore="NewApi">@color/white</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>
    Copy the code
    • Notice that in the manifest file
    <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>
    Copy the code
  • After processing, the effect of instant blank screen will not appear when the App starts
    • Set the theme to the theme of the Activity that is launched, and windowBackground is the Preview Window that will be shown. A splash can be a whole image or an XML file that can parse the image resources.
  • The program pays attention to key points
    • The background image for Preview Window will remain in memory if left unprocessed, so don’t forget to leave the background image blank when you go to the welcome page
    @override protected void onCreate(@nullable Bundle savedInstanceState) {// Set the window background to null getWindow().setBackgroundDrawable(null); super.onCreate(savedInstanceState); }Copy the code
  • How does this fit on the screen?
    • SplashActivity loads the image using the style style. You can’t set the zoom function like imageView, so you can use.9 images.
    • In the past, some developers have adopted my suggestion to directly set up the graph, without adaptation, and it does not matter, depending on the SPECIFIC UI requirements!

2.3 The second solution

  • Disable loading Preview Window as follows:
    <style name="SplashTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDisablePreview">true</item>
    </style>
    Copy the code
    • Disable Preview Window by setting it to the theme of the Activity to be launched, although similar effects can be achieved by setting the Preview Window to fully transparent. Personal feeling this kind of method does not have the first kind good!
  • The role of windowDisablePreview
    • By setting the android: windowDisablePreview properties, disable animation preview of the window, before SplashActivity display, system will never use the theme of the window to display its preview, it also ensures that there will be no white or black screen. However, and set the android: windowIsTranslucent attribute, if the SplashActivity started, there are too many complicated operation, will appear in the mobile phone click the application icon, but the bad n seconds will open the application before the card effects.
  • Are there any drawbacks to the scheme?
    • One minor drawback of this method is that there is no response for a few hundred milliseconds after the click, which is like “suspended animation”. It takes a while to jump out of the first Activity in our App. If you don’t want your App to have this brief “suspended animation” time, we recommend using the first method.

2.4 Points to Note

  • Either way, it will solve the problem. Note that the title bar and status bar of some mobile phones will also affect these two layers, causing the effect of shake, to avoid this situation need to deal with the status bar problem. Here you can directly reference my package status bar library, interested in understanding, directly used: github.com/yangchong21…

03.Application startup speed optimization

  • Improving the startup speed of APP and speeding up the execution time of Application is also a very important aspect. Here, I temporarily summarize several principles:
    • Try not to put some business logic in the Application;
    • Application does not store Application data as static variables.
    • If the size of App is not too large, there is no need to use dex subcontracting scheme;
    • Time consuming operations such as files and databases are handled in the IntentService thread
    • Don’t do anything about loops

04. Launch page mask back button

  • Generally, some initial configuration will be performed on the startup page of the App, so users are not expected to exit the App by pressing the back button when the startup page is loaded. Therefore, the back button can be shielded in the startup page. Here is a brief introduction to the specific implementation:
    /** * Activity blocks the physical return button ** @param keyCode * @param event * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    Copy the code

About other content introduction

01. About blog summary links

  • 1. Tech blog round-up
  • 2. Open source project summary
  • 3. Life Blog Summary
  • 4. Himalayan audio summary
  • 5. Other summaries

02. About my blog

  • My personal website: www.yczbj.org, www.ycbjie.cn
  • Github:github.com/yangchong21…
  • Zhihu: www.zhihu.com/people/yczb…
  • Jane: www.jianshu.com/u/b7b2c6ed9…
  • csdn:my.csdn.net/m0_37700275
  • The Himalayan listening: www.ximalaya.com/zhubo/71989…
  • Source: China my.oschina.net/zbj1618/blo…
  • Soak in the days of online: www.jcodecraeer.com/member/cont.
  • Email address: [email protected]
  • Blog: ali cloud yq.aliyun.com/users/artic… 239.headeruserinfo.3.dT4bcV
  • Segmentfault headline: segmentfault.com/u/xiangjian…
  • The Denver nuggets: juejin. Cn/user / 197877…