Android app startup mode, start-up process and start-up optimization analysis _android

Source: Internet
Author: User
Tags mremote

This article introduces some of the relevant knowledge of Android app startup, including app startup, app startup and App startup optimization!

App application startup mode

1. Cold start

When the application is started, the background does not have the application process, then the system will re-create a new process assigned to the application, this startup mode is cold start. Cold start because the system will re-create a new process to assign it, it will first create and initialize the application class, then create and initialize the Mainactivity class (including a series of measurements, layouts, drawings), and finally display it on the interface.

2. Hot start

When you start an application, there is already a process for that application in the background (example: Press the back key, the home key, although the application will exit, but the application process is still kept in the background, can go to the task list to view, so in the case of existing processes, this startup will start the application from the existing process, this way called hot start. Hot start because it will start from the existing process, so hot boot will not go application this step, but go directly mainactivity (including a series of measurement, Layout, drawing), so the hot start process only to create and initialize a mainactivity on the line, Instead of creating and initializing application, the application is initialized only once, because one application is created from the new process to the process's destruction.

App application startup process

For ease of typesetting, remove the parameters from some of the methods:

  1. Starting with the StartActivity () method of the activity class, this method invokes the public void Startactivityforresult () method in the Activity class
  2. The Startactivityforresult () method calls the public Activityresult execstartactivity () method in the instrumentation class, which is added {@hide} is invisible to the outside world.
  3. The Execstartactivity () method has the following invocation statement Activitymanagernative.getdefault (). StartActivity (), It calls the StartActivity () method in the Iactivitymanager class
  4. But Iactivitymanager is actually just an interface, where it actually calls the public abstract Classactivitymanagernative class (it inherits from the Binder class) The public int startactivity () method in the internal class Activitymanagerproxy, Activitymanagerproxy implements the Iactivitymanager interface
  5. The public int startactivity () method of the Activitymanagerproxy class has the following code, Mremote.transact (start_activity_transaction, data, reply, 0), where Mremote is a IBinder object, which is instantiated in the Activitymanagerproxy constructor method and is actually static public by the external class activitymanagernative Iactivitymanager asinterface (IBinder obj) method is instantiated, the parameters in the Asinterface (IBinder obj) method are actually static in the Activitymanagernative class Public Iactivitymanager Getdefault () method, instantiated by Servicemanager.getservice ("activity")
  6. Mremote.transact (start_activity_transaction, data, reply, 0) This statement passes the IBinder transact () method, Passing parameters in a method across processes to the Activitymanagerservice class
  7. All except Activitymanagerservice classes are located under the Android.app package
  8. Below enter the Activitymanagerservice class, it is located in the source code/frameworks/base/services/java/com/android/server/am/path, Package name is com.android.server.am
  9. Activitymanagerservice inherits the Activitymanagernative class, passing arguments from the mremote.transact () of the Activitymanagerproxy class, The Ontransact () method that is passed to the Activitymanagerservice class to handle the
  10. The Ontransact () method of the Activitymanagerservice class actually passes through Super.ontransact (code, data, reply, flags) This statement also calls the Ontransact () method in the Activitymanagernative class
  11. Super.ontransact (Code, data, reply, flags) This statement calls to the public final int startactivity () method of the Activitymanagerservice class
  12. The StartActivity () method of the Activitymanagerservice class calls the Startactivitymaywait () method of the Activitystack class
  13. The Activitystack class is located under the com.android.server.am package, Startactivitymaywait () method final int startactivitylocked () method
  14. The final Boolean resumetopactivitylocked () method is finally invoked by the startactivitylocked () method
  15. The resumetopactivitylocked () method invokes the private final void startspecificactivitylocked () method
  16. The startspecificactivitylocked () method calls the Startprocesslocked () method
  17. The startprocesslocked () method calls the public static final int start () method of the Android.os.Process class
  18. int pid = Process.Start ("Android.app.ActivityThread", msimpleprocessmanagement? App.processName:null, UID, UID, Gids, D Ebugflags, NULL)
  19. Below into the process class under the Android.os package
  20. The start () method invokes the private static int Startviazygote () method
  21. The Startviazygote () method invokes the private static int Zygotesendargsandgetpid () method
  22. The Zygotesendargsandgetpid () method communicates with the zygote process using the socket
  23. Szygotesocket = new Localsocket ();
  24. Szygotesocket.connect (New Localsocketaddress (Zygote_socket, LocalSocketAddress.Namespace.RESERVED));
  25. Below enter the Zygoteinit class under the Com.android.internal.os package
  26. The Zygoteinit class contains an instance of Localsocketserver, which communicates with the Zygotesendargsandgetpid () method mentioned above using a socket
  27. The actual logic is in the Boolean runOnce () method of Zygoteconnection this class
  28. The RunOnce () method calls the static method in Dalvik.system.Zygote this class forkandspecialize ()
  29. Below enter the Zygote class in the Dalvik.system package
  30. Forkandspecialize () Finally called the native method native public static int forkandspecialize ()
  31. The process of opening an application in C code
  32. The application process starts with the Activitythread class under the Android.app package
  33. The Activitythread class contains the main () method
  34. Activitythread.main () is the startup portal of the application, which is invoked when the application is started

startup optimizations for apps:

Based on the above boot process we try to do the following points

    1. Minimal time consuming operations during application creation
    2. If you use Sharepreference, try to operate in an asynchronous thread
    3. Reduce the level of layout and minimize time-consuming operations in the method of life cycle callbacks

Through this article, I hope to help everyone, thank you for your support to this site!

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.