Android Learning Memo

Source: Internet
Author: User

It is easy to forget the concept of the frame layer, and write a memo to make a note of the focus of understanding and the doubts encountered.

Context: With Android first contact with a class, the use of very very wide, in all places to use, such as Toast, Dialog, New TextView (), GetResource (), and so on, often have a parameter is the context. At the beginning of the object is very confused, translation came to be "context", not good understanding. In fact, most of the time we use the context, is the activity itself. The context of get to in view is actually the activity that this view is located in. The activity inherits from the Contextthemewrapper,contextthemwrapper inherits from the Contextwrapper,contextwrapper inherits in the context. In addition, when using the service, there is also a context, because the service is inherited from the Contextwrapper, the context is the service itself. There is also a global context:application. Obtained through Getapplicationcontext.

Contextimpl: Although activity inherits from the context class, the actual implementation of the context class method is not the activity itself, but the Contextimpl class. The class is also inherited from the context class, and within each activity there is a Contextimpl object that is created when the activity is created. Decorative mode.

Activitythread: A very, very important class. This class manages the life cycle of an application and is the gateway to the application. Each application corresponds to a Activitythread object, even our entire Android operating system, as well as a Activitythread object. At the same time, Activity, Service, and application are all created in this class.

Binder mechanism: cross-process communication mechanism, the core of Android system. The first to see the concept was to learn how to use AIDL to share data across applications, until later, after looking at some of the relevant data from the framework layer, to understand how important it is to play in Android. In Android, every app you launch, every activity you open, you need to interact with the Android system, specifically, to interact with the various services in your Android system, and every step of your application needs to be authorized by these services for management to work properly. These services are in another process, interacting through the binder mechanism. Take StartActivity as an example, in this process, the application first passes its own action through the binder mechanism to the activitymanagerservice (actually called a Activitymanagerservice method), Activitymanagerservice after a series of operations, agreed to the request, and internally registered the activity to start, and then through the binder mechanism called the Activtiythread to create the activity method, Activitythread This only starts instantiating the activity class and then callbacks the activity class's OnCreate, Onresume, and the like. Since Activitymanagerservice records every activity that is initiated, it can maintain all the activity of the entire system.

The abbreviation of Ams:activitymanagerservcie. Manage the service with activity, and the activity's life cycle is dispatched through it.

Windowmanagerservice: The service of the Management window (View), the drawing of the interface is dispatched by it, and the trigger of the event is passed to the specified View.

Attach method of activity: Because the activity instance is created dynamically through the reflection mechanism, its constructor and the actual initialization and assignment operation, but after the creation through the Attach method to initialize and the necessary assignment operation. Here's how.

Attach (context context, Activitythread Athread,              int  ident,              application application, Intent Intent, activityinfo info,              charsequence title, Activity parent, String ID,              Object Lastnonconfigurationinstance,                Configuration config)

The method is called in Activitythread, and the parameters are described below

Context:contextimpl instance, previously said that the real implementation of activity for the context interface goes to the internal Contextimpl variable implementation, is here to assign value. An Contextimpl instance is created at the same time when the activity is created.

Athread: Each application corresponds to a activitythread instance, where Athread is the Activitythread instance that the activity is applied to. Since activity is created and attach in Activitythread, this parameter is this.

InStr: This type of translation came to the "detection instrument", more difficult to understand what the meaning. However, in startactivity, it is used to send requests to AMS, which should be a tool class for interaction between the application and the system service, which assigns it to the instrumentation variable inside the activity. In addition, this variable is actually a private variable of activitythread, which is created when instantiating activitythread, so all activity within an application is shared with a instrumentation variable.

Token:ibinder object, this is the most difficult point to understand. If you understand the binder mechanism, you know that the IBinder object is actually a reference to another in-process binder service, and the method that calls it is equal to the method of invoking the binder service, and the various services within the Android system, such as AMS, Windowsmanagerservice, Notificationservice, Locationservice and so on are all so realized, collectively referred to as the service. But more often, the binder mechanism is not used for complex functions such as "services", but simply for a simple cross-process invocation, as is the case with the IBinder object here. Previously said, StartActivity need to register to AMS, after the successful registration, how can AMS notify the application of Activitythread create activity? In fact, when startactivity, the parameters passed to AMs have a Binder object: Applicationthread. To the AMS end becomes a IBinder object, after the activity registration is successful, AMS will call this IBinder object Schedulelaunchactivity method, This is equivalent to invoking the Schedulelaunchactivity method of the Applicationthread object within the application, which sends a message to the handler of the application's main thread, Finally, the Performlaunchactivity method of Activitythread is executed, and the activity is created and assigned in this method. The token here is a reference to a binder object created on the AMS side, noting that it is not a reference to the AMS service, but rather a binder object created by AMS for each registered activity, type token, Inherited from Iapplicationtoken.stub, which is created at the same time as the Activityrecord object is created, the name knows Activityrecord corresponds to an activity record. What's the use of this token object? More often, it exists as a key to find Activityrecord, and Token/activityrecord is stored as a key-value pair in AMS. In addition, the comment of this variable in the source code is: Window Manager token. There may also be a use for communication with the Windowmanagerservice, and then after seeing Windowmanagerservice related contentFilled. Usage supplement: As AMS needs to manage activity, it is normal to upload its references to AMS for each activity created, and token to manage it, because each token corresponds to an activity, There is no need to transfer the activity to come.

Ident: The essence is that the AMS side calls the System.identityhashcode (R), R is the Activityrecord object, and the hash value is obtained through the memory address. This object is what to use, temporarily do not see, then Add.

Application: Apply the corresponding application. This is very simple, and often used, the activity of the Getapplication method to obtain this value, the same, an application has only one application object, different activity within the Application object is the same.

Intent: Although this intent is transmitted from the Activitymanagerservcie end, its value should be the value of startactivity. Note that the re-instantiation of New Intent (R.intent) on the AMS side is not the same.

The Info:activityinfo object, which is passed by AMS, is the content of the activity node in the Androidmanifest.xml file, and for the parsing of the Androidmanifest file, the AMS server instead of the application side. Contains a series of configuration content, such as topics, loading methods, and so on. It is useful, for example, to use the theme to set Contentview.

Title:activity title. This is obtained on the application side, and is obtained by invoking Packagemanager with the Activityinfo object.

Parent: For a long while did not find out what this thing to do with, because in the normal startactivity process, the parent parameter is not assigned to the AMS side, and is not assigned value on the application side, Then the global search found it only in a Startactivitynow method to assign value, on StackOverflow on a check, originally used in the early use of tabactivity, This variable of the activity inside the tabactivity has a value that points to tabactivity. At the same time, the activation of internal activity is not through AMS. The technology has now been phased out, and it is not possible to pay attention to the use of this variable.

ID: This ibid, also used in Tabactivity, is the unique ID of the activity in the tabactivity for the surface.

Lastnonconfigurationinstances: This also ibid, is also used in tabactivity, I did not check the specific use.

config:configuration config = new Configuration (mcompatconfiguration), assigned by Activitythread Mcompatconfiguration object, You can assume that all the config values for an in-app activity are the same. The Configuration object records the type of hardware configurations for the phone, such as the keyboard type and whether the touchscreen is being touched.

Android Learning Memo

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.