How does Android start an Acitivity ?, Androidacitivity

Source: Internet
Author: User

How does Android start an Acitivity ?, Androidacitivity

The startup process can be divided into client and server.

I,ClientStartActivityOngoing work

Note: I personally understand,ClientEnd refers to the relative backgroundActivityManagerServiceA process visible to the foreground user, for example, starting a certain process on the InterfaceActivityIsLaucherProcess. In this exampleHomeStartActivityTo analyze the startup process:

(1) The main thread of the Launcher process, after capturing the onClick Click Event, calls the startActivitySafely method, and the startActivitySafely method internally calls

StartActivity method. The startActivity method then calls the startActivity method of the parent Activity of Launcher;

 

(2) Activity. the startActivity method calls the startActivityForResult method. The requestCode parameter passed in to this method is-1, indicating that Lanucher does not need to be executed after the Activity is started successfully. the onActivityResult method processes the returned results. The startActivityForResult method must call the execStartActivity method of Instrumentation. The Instrumentation object is used to monitor the interaction process between applications and systems (mainly Activity Manager.

(3) The execStartActivity method will send information about the Activity to be started to ActivityManagerService. If ActivityMonitor checks the Activity to be started, it will call ActivityManagerNative. getDault gets the ActivityManagerProxy object, and then calls the startActivity method of the proxy object .;

(4) The startActivity method that calls the ActivityManagerProxy proxy object actually calls BinderProxy. transact to send the START_ACTIVITY_TRANSACTION command to the Binder driver. At this time, the Lanucher process is switched to the ActivityManagerService process.

 

Summarize the above four steps: SourceAcTivity (Here isLauncher)The process uses a series of methods ,(Whether it is throughLauncherTo startActivity, Or passActivityInternal callStartActivityInterface to start a newActivit), All passBinderInter-process communication entersActivityManagerServiceProcess and callActivityManagerService. startActivityInterface; 


II,LanucherProcess ProcessingActivityIsClientToServerTerminalActivityManagerService. YesActivityManagerServiceProcess.

 

The processing process is divided7Stages

1The first stage -- pre-start check

In this phase, the main task is to check whether you have the permission to start the Activity, check whether the specified Intent Activity exists in the system, check whether the Intent is correct, and check whether the Activity can be switched, after various checks are passed, the ActivityRecord of the target Activity is created to determine whether there are reusable tasks or activities. If any, the ActivityRecord is associated. If none, the Task is created and the number of tasks in ActivityManagerService is updated, check whether any Activity is displayed on the top of the rollback stack. If yes, the Activity is paused.

 

2, Stage 2 -- Pause

The main task in this phase is to pause the Source Activity of the target Activity. For example, if I started Activity B from Actvity A, I need to pause Activity A before starting Activity B,

For subsequent processing of paused activities, either the destroy operation is executed or the operation is saved to the waiting stop List;

 

3Stage 3: Create a targetActivityProcesses running

Prepare the switchover animation and query whether the specified process information exists in the mProcessNames variable of ActivityManagerService. If any, reuse the information and directly start the Activity. If not, start a new process through zygote. Generally, the process startup timeout is 10 s.

 

4Stage 4: load the applicationActivity

The process entry is the main method of ActivityThread. The main function of the main method is to set the temporary process name, create the UI main thread ActivityThread, call its attach method, and finally enter the message loop of the main thread. Next we will disable hardware acceleration for low-memory devices, create an Application for the Application, initialize the Application, install Content Provider, execute the onCreate method of Instrumentation, and execute the onCreate method of Application.

 

5. Stage 5-display Acitivity

In the process where ActivityManagerService is located, the LANUCHER_ACTIVITY message is sent to the message loop of the main thread of the application, and then the onCreate, onStart, and onResume methods of the Activity life cycle are called back in the main thread to display the Activity, after the onResume method is executed, you need to add the Task where the current display Activity is located to the list of recent tasks before returning ActivityManagerService for subsequent processing. (The onNewIntent and onActivityResult methods may be called back before the onResume method)


 

6. Stage 6: Process Activitiy in idle state

 

This includes removing the timeout MSG in the ActivityStack message loop, stopping the list to be stopped, and destroying the Activity to be destroyed. if the Activity is in the system startup stage, an ACTION_BOOT_COMPLETEED broadcast is also sent, reclaim application processes.The most important operation is to stop the source.ActiBytes.


Note: This article is the result of reading books. It is mainly summarized in Lao Luo's Android source code scenario analysis and Yang yunjun's Android design and implementation I.



How does one know which activity is first displayed when a program is opened for multiple android activities?

This can be configured in the manifest file.

How can I start different activity classes in a project when developing an android program? How to Set it? Startup parameter?

First, I do not know your requirements.
In my understanding:

1. You do not need to start ac1.java, but choose to start ac2.java. In this case, you can change the startup class in AndroidManifest. xml. If the class name of ac2.java is TwoActivity,
Change (class name...) to TwoActivity.
<Activity
Android: name = ". (class name ..)"
Android: label = "@ string/app_name"
>
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>

2. You need to start both ac1.java and ac2.java. I don't think you can start ac1.java at the same time. After all, you have a single screen and it is impossible to display the content of two screens. Unless you have a choice, ac1.java is displayed in the first case; ac2.java is displayed in the second case. However, one problem exists. This if statement is written there, and xml cannot be written, so it can only be written in the java file. That is to say, the codes of ac1.java and ac2.java should be combined and written in A. java file.
You can create a new Activity. You have two layout XML files one. xml and two. xml. In the onCreate method of this Activity
If (Case 1 ){
SetContentView (R. layout. one );
} Else (Case 2 ){
SetContentView (R. layout. two );
}
In this way, you can select two la S. in Case 1, initialize the variables in Case 1, and in Case 2, initialize the variables in Case 2, and write them separately.

3. Start two files sequentially, jump from ac1.java to ac2.java, and use startActivity (intent) in ac1.java to jump.

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.