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:
- Starting with the StartActivity () method of the activity class, this method invokes the public void Startactivityforresult () method in the Activity class
- The Startactivityforresult () method calls the public Activityresult execstartactivity () method in the instrumentation class, which is added {@hide} is invisible to the outside world.
- The Execstartactivity () method has the following invocation statement Activitymanagernative.getdefault (). StartActivity (), It calls the StartActivity () method in the Iactivitymanager class
- 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
- 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")
- 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
- All except Activitymanagerservice classes are located under the Android.app package
- 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
- 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
- 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
- Super.ontransact (Code, data, reply, flags) This statement calls to the public final int startactivity () method of the Activitymanagerservice class
- The StartActivity () method of the Activitymanagerservice class calls the Startactivitymaywait () method of the Activitystack class
- The Activitystack class is located under the com.android.server.am package, Startactivitymaywait () method final int startactivitylocked () method
- The final Boolean resumetopactivitylocked () method is finally invoked by the startactivitylocked () method
- The resumetopactivitylocked () method invokes the private final void startspecificactivitylocked () method
- The startspecificactivitylocked () method calls the Startprocesslocked () method
- The startprocesslocked () method calls the public static final int start () method of the Android.os.Process class
- int pid = Process.Start ("Android.app.ActivityThread", msimpleprocessmanagement? App.processName:null, UID, UID, Gids, D Ebugflags, NULL)
- Below into the process class under the Android.os package
- The start () method invokes the private static int Startviazygote () method
- The Startviazygote () method invokes the private static int Zygotesendargsandgetpid () method
- The Zygotesendargsandgetpid () method communicates with the zygote process using the socket
- Szygotesocket = new Localsocket ();
- Szygotesocket.connect (New Localsocketaddress (Zygote_socket, LocalSocketAddress.Namespace.RESERVED));
- Below enter the Zygoteinit class under the Com.android.internal.os package
- The Zygoteinit class contains an instance of Localsocketserver, which communicates with the Zygotesendargsandgetpid () method mentioned above using a socket
- The actual logic is in the Boolean runOnce () method of Zygoteconnection this class
- The RunOnce () method calls the static method in Dalvik.system.Zygote this class forkandspecialize ()
- Below enter the Zygote class in the Dalvik.system package
- Forkandspecialize () Finally called the native method native public static int forkandspecialize ()
- The process of opening an application in C code
- The application process starts with the Activitythread class under the Android.app package
- The Activitythread class contains the main () method
- 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
- Minimal time consuming operations during application creation
- If you use Sharepreference, try to operate in an asynchronous thread
- 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!