What is Context and interview context in common interviews?

Source: Internet
Author: User

What is Context and interview context in common interviews?

Today's interview questions are the second in our common interview series. Let's talk about the context in android. I believe that all android Developers are very familiar with this context and must have used it before. But what is the context? How many people can understand it? The previous question is about listview, which is also very common. Today's context is also very common, so the interview questions are actually very close to us and must be common, few companies interview things that are very remote and don't need. What is not used in development? Right, well, it's a bit too much. We'll come back from the eggplant field and talk about this context.


1. What is context?

What are the most familiar and unfamiliar friends in our development? It may be a scene or context, but how many people understand this principle? I would like to briefly summarize the following points:

① It describes the information of an application environment, that is, the context.

② This class is an abstract class. Android provides the specific implementation class of this abstract class.

③ We can use it to obtain the resources and classes of the application, as well as some application-level operations, such as starting an Activity, sending broadcasts, and receiving Intent information.

After talking about these three points, it seems that you are still confused. I will summarize them.

Summary: Context is an abstract base class. We use it to access the resources of the current package (getResources, getAssets) and start other components (Activity, Service, Broadcast) and get various services (getSystemService). Of course, only the above content can be obtained through Context. To understand the Context, Context provides an application running environment. In the Context environment, an application can access resources to complete interaction with other components and services, context defines a set of basic functional interfaces. We can understand it as a set of standards, and Activity and Service are subclasses that implement this set of standards. This may not be accurate, because this set of specifications is actually implemented by the ContextImpl class in a unified manner, the Activity and Service only inherit and selectively override the implementation of some specifications.


2. Let's take a look at the inheritance relationship of the context-related classes.


We can see from the figure that Activity, Service, and Application classes are essentially Context subclasses. Do you know why context is always used in these classes?


3. Differences between Application, Activity, and Service as Context

Similarities: Both of them indirectly inherit the Context.

Differences: first, let's look at their inheritance relationships. Through comparison, we can clearly find that the class inheritance relationships between services and applications are similar, while the Activity has another level of inheritance ContextThemeWrapper, this is because the Activity has the topic concept, while the Service has no interface Service, and the Application is an abstract thing, which is also presented through the Activity class. The true implementation of Context is in ContextImpl, that is to say, most of the method calls of Context are transferred to ContextImpl, and the three are created in ActivityThread, the core process of Activity startup is completed in ActivityThread. It should be noted that Application and Service creation is also completed in ActivityThread.


4. How many context exists in an application?

How many context exists in an application? This is the most common and frequently asked question in our interviews. After reading the above three analyses? Do you know how many of them are there?

The answer is obvious: the total number of Context instances = Service count + Activity count + 1 (the Context instance corresponding to the Application)

I believe everyone can understand it? I really don't understand. I just need to take a look at the source code analysis and have a deep understanding. After learning about the content I mentioned above, you have encountered problems with context during the interview. Basically, there is no problem.

After finishing the two questions, do you want to look forward to the third question?


I personally organized a high-end Android communication group. If you are interested, you can first share this article to your circle of friends, add me, and send it to your circle of friends. I will pull you in. If you learn more, you can receive the dry goods push immediately.


No.: smart_android (persistent copy)

Introduction: A non-famous programmer, who has the character "straight left" and "right", loves machine and programming. He is a programmer crawling on the mobile Internet!

Personal ID: loonggg

Weibo: qilang

QQ group: 413589216 more information and source code are available in QQ group files

Toutiao: search for "non-famous programmers" to subscribe to more information

Work: focus on the development and research of mobile Internet. I am dedicated to sharing my experience in IT technology and programmers. You are welcome to pay attention to and repost.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.