Android Kernel Analysis Chapter 2 Understanding Context

Source: Internet
Author: User

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:

  1. 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;
  2. Contextwrapper is only a packaging class. Similarly, it does anything. It simply forwards requests to the contextimpl instance to complete specific logical execution;
  3. Contextthemewrapper supports theme-related topics. The theme can be specified for the application and activity elements through the Android: Theme tag in androidmanifest. xml;
  4. 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;
  5. 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;

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.