Context is widely used in our actual development, such as startactivity (...) /Getresources ()/getsharedpreferences (...) /Getsystemservice (.) in the book, context is understood as an Application Scenario. An activity instance is an application scenario, and a service instance is also an application scenario, but it only has a front-end interface; the inheritance relationships between classes related to context are as follows:
- Context is just an abstract class, and the specific logical implementation is all completed by contextimpl; the creation of contextimpl instances is almost completed in activitythread, such as handlebindapplication (...) /Createbasecontextforactivity (...) /Handlecreateservice (...) Load the APK application, activity, and service respectively;
- Contextwrapper is only a packaging class. Similarly, it does anything. It simply forwards requests to the contextimpl instance to complete specific logical execution;
- Contextthemewrapper supports theme-related topics. The theme can be specified for the application and activity elements through the Android: Theme tag in androidmanifest. xml;
- The foreground interface corresponding to the activity. Therefore, you need to use the topic. The background service does not have an interface and does not need a topic. Therefore, you can directly inherit contextwrapper;
- An Application object is the first object created when an application is started for the first time. Each application has only one application object, which represents a broad application scenario and is not related to the interface, therefore, contextwrapper can be directly inherited. developers can inherit this class to implement more business logic, such as adding resources or services to be loaded as the system starts;
How many contexts are there in the system?
From the above description, we can see that the number of context = 1 application + N activities + N services;