Analyze Context and Memory leakage problems, and analyze context
What is Context
In English, Context indicates the Context, environment, background, and so on ...... So what is the relationship between the Context meaning in Android and these English interpretations? Let's take a look at the definition given by Google:
Interface to global information about an application environment. this is an abstract class whose implementation is provided by the Android system. it allows access to application-specific resources and classes, as well as up-callfor application-level operations such as launching activities, broadcasting and processing intents, etc.
I don't think it is necessary to translate it completely. What is the core meaning of this passage:
What can Context do?
Let's look back at the use of Context to help us understand it: we need to input a Context parameter when using a custom View, BaseAdapter, or even accessing a database file, have you ever wondered why? Because we need to associate the View with a page layout when initializing a custom View, because we need a layout file as a sub-Item when using BaseAdapter, because we need to access the database file of the application. At this time, Context is like a system information administrator. If you tell me that you want to access the layout resource file of the system and the database file of the application, you can find it for you, and then provide reference for you to use.
So now we can understand that Context is used to obtain system resources. The system resources we call include the information of the device, there are also information provided by our developers (classes, packages, resource files, etc ......).
Context class inheritance Diagram
Before analysis, let's take a look at the class inheritance diagram of Context to understand the main implementation classes of Context:
As you can see in, the Application class and the Activity and Service Android components are both specific implementation classes of Context, and these three classes can indeed access the system resources within their respective responsibilities, for example, an Activity can access resources related to interface elements, services can access system services, and applications can access Application package names.
Knowledge Point: Number of Context in an Application = number of activities + number of services + 1 (number of applications)
What you don't know when using Context
As mentioned above, the Context implementation class can hold a variety of system information. May someone think that using Context will not cause memory leakage? Here I will give you an answer: Yes! In addition, we often inadvertently cause memory leakage during development. Some people may be dissatisfied. How can I write code that may cause memory leakage!
Don't worry. Let me show you the most common code:
For example, we want to implement a ListView display function. to customize sub-items, we need to input Context to read layout resources:
Public class MyBaseAdapter extends BaseAdapter {private Context mContext; public MyBaseAdapter (Context context) {mContext = context;} omitted ......}
After the BaseAdapter is designed, we must use it in MainActivity:
Protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); adapter = new MyBaseAdapter (this );...... Omitted}
Generally, you will write such code, right? In this way, no bugs will occur, right? The boss praised you for your high work efficiency, right? In fact, such code may cause memory leakage ~ Why? You may wish to reviewMemory leakage caused by Thread usage in ActivityAs mentioned in the memory leakage reason, the spam mechanism will not recycle this object as long as the class holds the reference of the external class instance object.
In this Code, we pass in the reference of Activity to MyBaseAdapter so that MyBaseAdapter holds the reference to Activity. Accordingly, we will also hold the reference to the resource file obtained by Activity. Therefore, these resource files cannot be recycled by the garbage collection mechanism, resulting in Memory leakage.
So what are the solutions?
We only need to modify the following code:
MContext = context. getApplicationContext (); or adapter = new MyBaseAdapter (this. getApplication ());
To avoid Memory leakage. What you need to remember is that here we only demonstrate the most common memory leakage problem caused by the most basic Context. There must be more complex situations in actual development requirements, you should think more about the causes and solve these memory leaks from the root causes.