Detailed Android cold start to implement app second Open method _android

Source: Internet
Author: User
Tags transparent color

First, the preface

Before reading this article, you need to understand a few things first:

1. What is the cold start time for Android?

Cold start time is the time between when the user clicks the app from the desktop of the mobile phone and the Activity calling method of the startup page onCreate() .

2. What happened during the cold start time period?

First we need to know what happens when you open one, and Activity Activity if the one that belongs to is Activity Application not started, then the system Activity creates a process for this (every process creates a call Application , so Application the onCreate()method may be invoked multiple times, in the process creation and initialization, it is bound to consume some time, in this time, WindowManager will first load the app in the Theme Style window background (windowbackground) as a preview element, and then go to really load the layout, If this time is too long, and the default background is black or white, this will give users an illusion, this app is very card, very not fluent, nature also affected the user experience.

Look at the effect chart:


Not optimized


Optimization Scenario 1


Optimization Scenario 2

Second, eliminate the start of the screen/black screen

When the user clicks on the Mobile desktop app, see the black screen or white screen is actually the first frame before the interface rendering, if you read the article head of the 2 questions, then solve the problem is very easy, nothing more than the theme in the windowBackground set into our want to let users see the screen can be, Here are 2 ways to do this:

1, set the background map to our app logo, as the app boot, and now most of the apps on the market are doing the same.

  <style name= "Appwelcome" parent= "Apptheme" >
    <item name= "Android:windowbackground" > @mipmap/bg_ Welcome_start</item>
  </style>


2, set the background color to transparent color, so that when users click on the Desktop app picture, and will not "immediately" into the app, and stay on the desktop for a while, in fact, this time the app is already started, but we are scheming to theme in the windowBackground color set to Transparent, Forced to dump the pot to the mobile phone application manufacturers (mobile phone response is too slow, haha), in fact, now micro-letter is also done, do not believe you can try.

  <style name= "Appwelcome" parent= "Android:Theme.Translucent.NoTitleBar.Fullscreen"/>

One thing to note about transparency is that if you introduce theme directly Activity , the following exceptions may occur at run time:

Java.lang.IllegalStateException:You need to use a theme.appcompat Theme (or descendant) with the this activity.

This is because the use of incompatible theme, such as my inheritance here Activity , the AppCompatActivity solution is simple:

1, let it Activity integrate Activity and not integration of compatibilityAppCompatActivity

2, in onCreate() the method super.onCreate(savedInstanceState) before setting up our original app theme

public class Mainactivity extends Appcompatactivity {
  @Override
  protected void OnCreate (Bundle Savedinstancestate) {
      settheme (r.style.apptheme);
      Super.oncreate (savedinstancestate);
  }

2 of the above approaches, we all need to introduce theme into the correspondingActivity

    <activity
      android:name= ". App.main.MainActivity"
      android:theme= "@style/appwelcome"
      android: screenorientation= "Portrait" >
      <intent-filter>
        <action android:name= " Android.intent.action.MAIN "/>
        <category android:name= android.intent.category.LAUNCHER"/>
      < /intent-filter>
    </activity>

Iii. about startup optimization

The above practice can actually achieve "seconds open" app effect, but it is not real speed, in the activity creation process is actually going through a series framework of layers of operation, in the daily development, we will go to rewrite the Application class, and then in the Application initialization of some operations, such as the static user identification TOKEN , the initialization of Third-party SDK and so on.

Here are a few suggestions:

1, do not let Application participate in the operation of the business

2, do not APPlication take time-consuming operations, such as some developers in their own app in a series of folders or files (such as myself), these I/O operations should be put "when it is really used to create" or some of the database operations.

3, do not use static variables in the way of Application saving data and so on.

Of course, this is the absolute idealism, the above "do not" 2 words before adding "as far as possible" 2 words, after all, in the actual development, it does make us a lot easier.

Yes, add, layout is also very important, as far as possible to reduce the complexity of the layout, layout depth, because in the View process of drawing, measurement is also very expensive performance.

OK, the above is the Android cold start to implement app seconds open all the content, I hope this article content for everyone to develop the app can help, if there is doubt can message exchange.

Related Article

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.