One of the three core functions of the Android kernel, the internal principle of AMS, androidams

Source: Internet
Author: User

One of the three core functions of the Android kernel, the internal principle of AMS, androidams

The above class is the full name of AmS, and the other two core functions are WindowManagerService. java and View. java.

 

AmS provides the following functions:

Unified management of memory management processes for each application

AmS defines several important data classesUsed to save processes, activities, and tasks)

ProcessRecord. java

The internal variables in this class can be divided into three parts: Process file information, process memory status information, and Activity and Service contained in the process.

This class is in the framework/base/services/java/com/android/server/am/path, and the am at the end of this path represents Activity Manager, important classes related to AmS are in this directory.

ActivityRecord. java

In AmS, The ActivityRecord data class is used to save the information of each Activity. History contains an int task variable to save the task to which the Activity belongs. You can use the Intent. FLAG_NEW_TASK identifier to tell AmS to recreate a Task for the started Activity.

TaskRecord. java

In AMS, the concept of task is used to ensure the sequence in which the activity starts and exits.

Important scheduling variables in AmS
// How long we wait until we timeout on key dispatching. no response timeout for buttons. This is google's standard. Most Lenovo mobile phones in China are designed according to this standard. Huawei mobile phones do not follow this standard static final int KEY_DISPATCHING_TIMEOUT = 5*1000;
// Maximum number of recent tasks that we can remember. static final int MAX_RECENT_TASKS = 20; Maximum number of AMS recorded activities. If the value exceeds 20, the Activitystatic final int BROADCAST_TIMEOUT = 10*1000 broadcast timeout value will be discarded.
Final ArrayList <PendingActivityLaunch> mPendingActivityLaunches = new ArrayList <PendingActivityLaunch> (); temporarily save client requests
StaticFinalInt PAUSE_TIMEOUT =500
AmS notifies the application process to suspend a specified Activity,
The patience of AmS is limited, only 500 milliseconds.
If the application process is stopped during the constant time,
AmS is forced to suspend and close the Activity.
This is why you cannot do too many things in onPause () during application design.

ActivityStack is the stack that stores activity information.

Related variables:

Final ArrayList mHistory = new ArrayList (); saves all running activities.

Private final ArrayList mLRUActivities = new ArrayList ();

LRU (Latest Recent Used) to save all previously Used activities

 

Unified scheduling of various application activities

Activity does not correspond to one application. ActivityThread corresponds to one application. Therefore, Android allows multiple applications to run simultaneously. In fact, multiple activitythreads can run simultaneously.

In Android, the basic idea of Activity scheduling is as follows: each application process must first report to AmS to start a new Activity or stop the current Activity, rather than "Unauthorized handling ". AmS records all application processes internally. When receiving a start or stop report, AmS First updates the internal record and then notifies the corresponding customer process to run or stop the specified Activity. Because AmS has records of all internal activities, it can naturally schedule these activities and automatically kill the background Activity based on the Activity and system memory status.

Summary by yourself: The activity starts or stops the notification AmS. The Ams decides whether to execute the action. When the number of activities is greater than 20, the earliest activity on the stack will be forgotten by AmS, and the newly added activity will be added to the stack.

Underlying Working Diagram of startActivity

 

Take click A-B as an example:
When you click A, when AmS receives the customer request startActivity (), it first suspends the current Activity. Therefore, it is necessary to determine whether the mResumedActivity is empty. In general, this value is not empty. If it is not empty, AmS will notify the client to pause, execute the Activity, and return.
When process A is suspended and AmS is reported, AmS starts to execute completePaused (). In this method, check whether the target Activity is in the mHistory list, it indicates that the target process is still running, the target Activity is only in the stop State, and there is no finish, so you can notify process B to directly resume the specified Activity.

Analysis of Four startup modes of Activity

  

Android: launchMode is set in the AndroidManifest. xml file.
Standard (default), singleTop, singleTask, and singleInstance
<Activity android: name = "com. example. android_launchmodel.Activity1 "android: launchMode =" standard "/> default <activity android: name =" com. example. android_launchmodel.Activity2 "android: launchMode =" singleTask "/> <activity android: name =" com. example. android_launchmodel.Activity3 "android: launchMode =" singleTop "/> <activity android: name =" com. example. android_launchmodel.Activity4 "android: launchMode =" singleInstance "/>
Standard

This is the default mode. Every time an Activity is activated, an Activity instance is created and placed in the task stack.

SingleTop

If an instance of the Activity exists at the top of the stack of the task, the instance will be reused (onNewIntent () of the instance will be called). Otherwise, a new instance will be created and placed on the top of the stack, even if an instance of this Activity already exists in the stack, a new instance will be created as long as it is not at the top of the stack.

SingleTask

If an instance of the Activity already exists in the stack, the instance will be reused (onNewIntent () of the instance will be called ()). When it is reused, the instance will be sent back to the top of the stack, so the instance on it will be removed from the stack. If this instance does not exist in the stack, a new instance will be created and put into the stack.

Add an instance and create four activity classes (as shown above in AndroidManifest. xml file activity2 is in singleTask mode) inOnCreate, onStart,OnPause, onStop, onDestroy method call log

The Activity startup sequence is

Activity1 à Activity2 à Activity3 à Activity4 à Activity2

 

 

When activity1 is on the stack -- activity2 is on the stack -- activity3 is on the stack -- activity4 is on the stack -- (enable activity2 in activity4 because activity2 is set to singleTask mode, the activity2 instance is found in the task stack. The next step is to place the activity2 instance on the top of the stack, but there are also activity3 and 4 instances on the activity2 instance. What should I do! They can only get them out of the stack .)

-- We can see that the onDestroy () Methods of activity3 and Activity4 are executed.

When Activity4 starts Activity2, The onCreate () of activity2 is not executed (this also means that AmS has not re-created the activity2 instance and put it into the task stack, oncreate () the method is called only when the activity is created for the first time)

It indicates that Activity3 and Activity4 in the stack have been released, and the Activity2 instance in the stack has been started.

Press the return key twice and then return to the desktop.

 

So far there are no instances of activity1 and activity2 in the stack (others are similar)

 

SingleInstance

Create an instance of the Activity in a new stack, and allow multiple applications to share the Activity instance in the stack. Once an Activity instance in this mode already exists in a stack, any application that activates the Activity will reuse the instance in the stack (onNewIntent () of the instance will be called ()). The effect is equivalent to sharing one application among multiple applications. No matter who activates the Activity, the Activity enters the same application. In this mode, no new Acitvity instances will be pushed into the stack. The rest is the same as 'singletask. This means that there is only one Activity instance in the stack. This is a very special mode, which can only be used in a program with only one Activity.

Reference other instances: http://blog.csdn.net/liuhe688/article/details/6754323

 

AmS memory reclaim rules

1. Foreground processes refer to the processes related to user operations. Including:

  • Activity that is interacting with the user
  • Contains a Service that is serving the Activity that interacts with users.
  • Contains a Service, which is executing the onCreate (), onStart (), onDestory () method.
  • Contains a BroadcastReceiver. The onReceive () function is being executed.

2. the visible process, although not interacting with the user, can affect what the user sees.

  • A dialog box located on Activity
  • One Service

3. Service Process: all processes of services started using startService () are called service processes.

4. Empty Process: The process does not contain any componet, including Activity, Service, and explorer objects. The reason for retaining these processes is to reduce the overhead required for re-creation.

 

Code meaning of Activity life cycle

In the past, during application development, most people have already understood the main states in the Activity life cycle and learned how to do different things in these states. However, there may be some questions, such as the real difference between the onStart () method and the onStop method. Although you know that onStop indicates the stop of the Activity, onStart indicates the start of the Activity. The question is what is the difference between start and stop?



 

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.