preface

AMS

Activity Manager Service (AMS) is a part of the system process and a Binder Server used to manage, record, and query information about running components. AMS is a system process that manages the running status of activities and components. Its main functions are as follows:

  1. Component State Management (Four major components)
  2. Component Status Query
  3. Task related operations
  4. Other operations (system running information, such as memory, CPU information, etc.)

Get the ActivityManager object to get the information. ActivityManager ams = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);

App Startup Process

The Android system is based on the Linux system. In principle, its applications are not just apK. Applications that can run on the Linux system can run on the Android system in some ways, and some system-level applications exist in this form.

There are usually two ways to launch an app:

  1. Click the app icon in the Launcher to launch
  2. Start with startActivity.

Startup process:

  1. AMS –> startActivity()
  2. ApplicationThread.schedulePauseActivity(),ActivityThread.handlePauseActivity()
  3. WMS –> remove from window
  4. The current ActivityactivityPaused,completePauseLocked
  5. Handle the process of starting a new Activity,attachApplication,realStartActivityLocked
  6. ApplicationThread.scheduleLaunchActivity(),performLaunchActivity
  7. Activity.attach(),Activity.onCreate()

1. Activity state management and Task management

Here refer to the official instructions, very detailed.

When the system stops one of your activities (for example, a new Activity is started or a task moves to the foreground), it may destroy the Activity entirely if the system needs to reclaim system memory resources. When this happens, information about the state of the Activity is lost. If this happens, the system will still know that the Activity is in the back stack, but when it is placed at the top of the stack, the system must rebuild the Activity (rather than resume it). To prevent users from losing their work, you should actively save it by implementing the onSaveInstanceState() callback method in your Activity.

Two, matters needing attention

Android can not only load many components, but also organize them into ActivityTasks across processes. This feature makes each App not isolated, thus maximizing resource reuse.

The Activity list describe
mHistory The history stack
mLRUActivities The least recently accessed Activity
mStoppingActivities Activity already stopped
mGoingtoSleepActivities An Activity that is about to enter the sleep state
mNoAnimActivities You don’t need to start an animated Activity
mFinishingActivities To perform thefinish()Methods the Activity of

If Activity A starts Activity B, Activity B can define in its manifest file how it should associate with the current task (if possible), and Activity A can request how Activity B should associate with the current task. If both activities define how Activity B should be associated with the task, Activity A’s request (as defined in the Intent) takes precedence over Activity B’s request (as defined in its manifest file).

The manifest file configuration affects tasks

attribute describe
taskAffinity A tasktaskAffinityThe value depends on its root Activity. This Activity is set when a new Task is createdtaskAffinityAttribute to determine which task it belongs totaskAffinityProperties.

You can configure Application or Activity properties in the manifest filetaskAffinityThe value rule complies with the package name rule (including at least one dot). The default value is the package name.
allowTaskReparenting Dynamic transfer, by defaultActivity ABoot will be placed in with boot itActivity BThe same stack, while another stack is moved to the foreground and started the previous oneActivity A, by defaultActivity AI’m going to keep it on the stack. If the Activity is set in the manifest fileandroid:allowTaskReparenting="true"Attribute value,Activity AIt will be moved to the stack where it is currently started.
clearTaskOnLaunch Only toroot ActivityValid, the value istrueStarting the Activity clears the target stack of activities other than itself
alwaysRetainTaskState If the user stops accessing the Task for a certain period of time, such as 30 minutes, the system will clear the Activity in the Task.root ActivityExcept), and sets the value totrue, can change the situation.
finishOnTaskLaunch Whether the Activity instance should be shut down each time the user starts its task again (selecting the task on the home screen) – true means it should be shut down, false means it should not be shut down. The default value is false.

If both this attribute and allowTaskReparenting are “true”, this attribute takes precedence.
noHistory The Activity will not be saved inHistory StackIn the.

The Intent Flag affects the Task at startup

Flag describe
FLAG_ACTIVITY_NEW_TASK Is equivalent tosingleTaskMode. The following flag needs to be used with this flag.

FLAG_ACTIVITY_CLEAR_TASK: Clears other activities in the stack.

FLAG_ACTIVITY_TASK_ON_HOMEThe newly launched Activity is placed at the top of the task stack Launcher.

FLAG_ACTIVITY_MULTIPLE_TASK: Prevents the system from recovering an existing task, that is, starting a new task each time.

FLAG_ACTIVITY_LAUNCH_ADJACENT: Only used in split-screen, multi-window mode. The newly started Activity is displayed next to the Activity that started it. If you need to create a new instance of an existing Activity, set this at the same timeFLAG_ACTIVITY_MULTIPLE_TASK.
FLAG_ACTIVITY_SINGLE_TOP Is equivalent tosingleTopMode.
FLAG_ACTIVITY_CLEAR_TOP If the Activity you want to start is already on the stack, you need to clear all activities on top of it and passonNewIntent()Method to pass the Intent to the Activity
FLAG_ACTIVITY_NO_HISTORY The Activity will not be saved inHistory StackIn the.
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS The Activity will not be placed in the list of recently started activities on the system.

If this page does not appear in the process list screenshot (a screenshot of the previous page will be obtained)

When you press the home button to return to the desktop and click the desktop icon to return to the foreground, the previous page is displayed instead of the same page.
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED Whether the Activity targets a new task or an existing task, it is always at the top of the task.

Generally speaking,FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESETThe identifier is used in conjunction with API 21,FLAG_ACTIVITY_NEW_DOCUMENTTo replace it.

When the Activity starts, if it already exists in the target stack, clear all activities on it.

Add: reference

The official documentation provides an in-depth understanding of the Android kernel design. Lin Xuesen SDK source code