Differences in AMS versions

There is a big difference between Android7.0 and Android8.0 in AMS communication.

On Android7.0, AMS mainly uses implementation proxy mode and Binder communication to realize the communication between APP and AMS.

On Android8.0, AMS uses the AIDL approach for communication, which is generally much simpler.

AMS

AMS launched

AMS is started in SystemService, and the run method is called in the main method of SystemService. The startBootstrapServices method is called in the run method, where AMS is started.

AMS will determine whether the process to which the Service or Activity belongs exists when launching Service or Acelasticity, and if it does not, will start the process first, before launching the Service or Activity.

There are many data structures in AMS, so let’s take a look at ActivityRecord, TaskRecord, and ActivityStack.

ActivityRecord

The ActivityRecord is used to record all information about an Activity, to describe an action that is created when an Activity is launched. The important member variables for ActivityRecord are as follows:

This is all important information for an Activity. ActivityRecord also contains TaskRecord, which is an important member of the Activity task stack.

TaskRecord

TaskRecord is used to describe an Activity task stack. Now let’s look at TaskRecord:

We can see a lot of important information in TastRecord, which also includes ActivityStack.

ActivityStack

ActivityStack is a management class that internally maintains the state of all activities in the system. ActicityStack is managed by an ActivityStackSupervisor, which is created in the constructor of AMS.

There are many ActivityStack instances in ActivityStackSupervisor, such as mHomeStack, mFocusedStack, and mLastFocusedStack.

  • MHomeStack: Store all the activities in your Launcher APP.
  • MFocusedStack: Represents all activities that are currently receiving input or starting the next Activity.
  • MLastFocusedStack: Represents all activities that previously received input.
ActivityState

The class that stores Activity state in ActivityStack is ActivityState. ActivityState is an enumeration of RESUMED,PASING,STOPPING, and so on. At the time of call overridePendingTranstition method will be based on the current state of the Activity to determine whether to need to start the animation.

Special state Activity

There are also activities defined in ActivityStack in special states.

  • MPausingActivity The Activity that is being paused
  • MLastPasedActivity A suspended Activity
  • MLastNoHistoryActivity Indicates the last Activity that has no history
  • MResumedActivity An Activity that has already resumed
  • MLastStarteActivity Indicates the Activity that was started last time
  • The top Activity of mTranslucentActivityWaiting passed to convertToTraanslucent method

ActivitiRecord saves activities in these special states.

Management of the Activity stack

Let’s take a look at the Activity stack model:

TaskRecord manages many activities, and ActivityStack manages many task stacks.

When an Activity is launched, you can set the launch mode in the Androidmanifest and Intent. If both are set, the Intent overrides the properties of the Androidmanifest. All the Activity launch properties set for the Activity are used when the Activity is started.