First, let's take a look at the definition of Activity source in the source code:
[Java]
Public class Activity extends ContextThemeWrapper
Implements LayoutInflater. Factory2,
Window. Callback, KeyEvent. Callback,
OnCreateContextMenuListener, ComponentCallbacks2 {
...
}
Next we will analyze the significance of each part in detail:
Extends ContextThemeWrapper indicates that the Activity is essentially a ContextThemeWrapper. What is ContextThemeWrapper? Check the definition of ContextThemeWrapper in the source code:
[Java]
Public class ContextThemeWrapper extends ContextWrapper {
...
}
It can be seen that ContextThemeWrapper is a ContextWrapper, continue to look down:
[Java]
Public class ContextWrapper extends Context {
Context mBase;
...
}
ContextWrapper is essentially a Context. The context is defined as follows:
[Java]
Public abstract class Context {
...
}
Shows the overall structure.
Context is an abstract class, so we can know that Activity is actually a Context and implements some interfaces. How can we understand Context?
Context is commonly known as Context. We use Context in many object definitions, such as ImageViewimageView = new ImageView (this). here this is the Context of the current Activity. The Source Code explains Context as follows:
Interface to global information about anapplication environment. this is an abstract class whose implementation isprovided by the Android system. it allows access to application-specificresources and classes, as well as up-callfor application-level operationssuch as launching activities, broadcasting and processing intents, etc.
Context is only an interface. The ContexImpl class is used to implement the Context function. To understand the specific functions of Context, let's first understand which interfaces are defined in Context:
[Java]
Public abstract ComponentNamestartService (Intent service );
Public abstract boolean stopService (Intentservice );
Public abstract void startActivity (Intentintent );
Public abstract void sendBroadcast (Intentintent );
Public abstract Intent registerReceiver (BroadcastReceiverreceiver, IntentFilter filter );
Public abstract Resources getResources ();
The above is a snippet of many Context interfaces. Are you familiar with the interfaces? In fact, some of the functions we often use are actually defined in Context, so Context can be considered as a class to encapsulate general functions. Of course, this class is not only for Activity, the most common service is inherited from Context, and a lot of classes are inherited from Context. For details, refer to Principal.
ContextWrapper is only a simple encapsulation of Context. to modify the Context, we only need to modify ContextWrapper, instead of modifying the general Context. The purpose of ContextWrapper is nothing more than that. ContextThemeWrapper only adds Theme-related content on the basis of ContextWrapper. For the Activity, Theme-related content needs to be processed, but for the Service, ContextWrapper must be inherited, because the Service does not need to process Theme-related content.
After analyzing the extends part, let's take a look at the implements Part. extends determines the nature of the Activity, which can be considered as an extension of the Activity.
LayoutInflater. Factory: the callback interface when LayoutInflater is used to inflate a layout.
Window. Callback: this interface is used by the Activity to process messages. This part involves message transmission and will be introduced in detail later.
ComponentCallbacks2: defines the interface for memory management. The interface for callback and processing when the memory is too low
KeyEvent. Callback: the Callback interface for keyboard event response, such as onKeyDown ().
OnCreateContextMenuListener: Listener interface for the context menu display event. This method is used to process some operations when the context menu is displayed.
Through the above analysis, we probably know what the Activity is, which will be helpful for understanding the Activity in the future.
Since I have just learned Anroid, please correct me if anything is wrong ~
[Java]
<Pre name = "code" class = "java"> <pre>
Author: zjmdp