App startup Mode

How do I launch my App? It’s all about clicking the App icon on the screen. But when you click on it, sometimes it’s fast, sometimes it’s slow, and sometimes there’s a blank screen in the middle. Sometimes there’s a black screen in the middle. What is the cause of this situation? Let’s start with the way the App launches.

Cold start Hot startCopy the code

Cold start

Cold start: When an application is started, the system creates a new process and assigns it to the application.

Cold start features: Because the system will create a new process assigned to it, it will create and initialize the Application, create and initialize its Launch Activity(onCreate onMesure onLayout,ondraw), and finally display on the interface

Warm start

Hot start: When an application is started, it is started from an existing process (the back button, the home button, the application exits but is not destroyed) that exists in the background

Hot start features: Start from an existing process, without creating or initializing the Application, directly create and initialize its Launch Activity

<mark> The difference between the two: whether the background process has corresponding process, creating a process is time-consuming operation </mark>

The startup process of the application

Application-> Create and initialize the Launch Activity.

You can refer to Luo’s Android journey -Android application startup process source code analysis

Here’s a summary of the App startup process:

The entire application startup process involves many steps, but as a whole, it can be divided into the following five stages: 1. Step1 -Step 11Binder interprocess communication notifies ActivityManagerService that it wants to start an Activity. twoStep 12 - Step 16: ActivityManagerService uses the Binder interprocess communication mechanism to tell the Launcher to go into Paused state; 3.Step 17 - Step 24: The Launcher uses the Binder interprocess communication mechanism to tell ActivityManagerService that it is ready to go to Paused, and ActivityManagerService creates a new process, It is used to start an ActivityThread instance in which the Activity to be started will run. Four.Step 25 - Step 27: ActivityThread passes a Binder object of type ApplicationThread to ActivityManagerService through the Binder interprocess communication mechanism. So that ActivityManagerService can later communicate with the Binder object; Five.Step 28 - Step 35The: ActivityManagerService notifies the ActivityThread through the Binder interprocess communication mechanism that it is now ready to actually start the Activity.Copy the code

<mark> If you are interested, you can take a look at the source code. In fact, the Launch of App is equivalent to clicking an icon in the Launch App to jump </mark>

ActivityManagerService creates a new process that starts an ActivityThread instance. And what is Zygote? I haven’t figured it out yet. I recommend two blogs for your own understanding of Android’s simple Zygote

Android system process Zygote start process source code analysis of the subsequent understanding, will be specifically explained under

How to measure application startup time

  1. You can drive piles through code to calculate startup time
  2. It can be calculated with a stopwatch (visually, this is very low)

Android has a command to calculate the startup time

adb shell am start -W [packageName]/[packageName.launchActivity]Copy the code

OK, so let’s take our own project and show you the difference between these two startup times

Cold start:





Cold start

Warm start





Warm start

<mark> The time difference between the two is large, which only means that many time-consuming operations are performed in the Application. And now most App launches are cold launches, because the user’s habit is not to have so many background processes, they like to delete all processes

Note: avoid time-consuming operations in the Application and reduce the LaunchActivity View hierarchy to reduce the View measurement drawing time

Why is the screen blank or black when the App starts

Back to the original question, why do I sometimes get a white/black screen when I open my App

First, explain why the white screen/black screen appears?

The background of the Windows window is displayed before loading the layout file because you have entered the Activity. A black/white screen is the Windows background that is displayedthemeThe Settings)Copy the code

Solution:

1.Set the background image for Theme. <style = "box-sizing: border-box! Important; word-wrap: break-word! Importantname="Theme.AppStartLoad" parent="android:Theme">  
    <item name="android:windowBackground">@drawable/ipod_bg</item> 
    <item name="android:windowNoTitle">true</item>  
</style>

2.<style = "Theme" > <style = "Theme" > <style = "Theme" > <style = "Theme"name="Theme.AppStartLoadTranslucent" parent="android:Theme">  
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowNoTitle">true</item>  
</style>Copy the code

Instead of expanding here, we can take a look at this Android development to solve the problem of white screen or black screen flashing when the APP starts

Tips for optimizing cold start times

Follow the solution to why the screen is white/black.

In fact, you can set the background of the Theme for the launched Activity as the picture of the launch page, and then restore the original Theme after loading the Activtity layout

Code examples:

In the style. The XML file

 <style name="AppTheme.Launcher">
     <item name="android:windowBackground">@drawable/appstart_background
     </item>

        <! --<item name="android:windowIsTranslucent">true</item>-->
    </style>Copy the code

In the AndroidManifest. XML

  <activity android:name=".AppStartActivity"
            android:theme="@style/AppTheme.Launcher">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>Copy the code

AppStartActivity.java

public class AppStartActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            Thread.sleep(3000);
        } catch(InterruptedException e) { e.printStackTrace(); } setContentView(R.layout.activity_appstart); }}Copy the code

The @drawable/appstart_background above is the default background for the launch page. Pro test, available

A link to the

Android Performance Optimization – Startup process Cold start hot start

Android cold boot hot boot test

Eliminate the problem of white screen or black screen when the activity starts

Android development to solve the problem of APP startup white screen or black screen flash