Android Context principles and usage

Source: Internet
Author: User

Android Context principles and usage


1. How the Context inheritance system and Context are created 1. Context Inheritance System
Remember: Activity, Service, and Application are inherited from ContextWrapper, while ContextWrapper implements Context. Each Activity, Service, and Application is a Context instance.

2. When to create the Context and how to create the Context-view the source code
Analysis on the process of creating the Context of the Android application window (Activity)

Android kernel -- Context object

Explanation of Context in Android ---- Context you do not know
The above three articles analyze when Context is created from the source code perspective. However, for normal development, you only need to know that both Activity and Service are inherited from Context, as long as a new Activity or Service instance is created, a new Context instance is created.
Total Context = Activity count + Service count + 1 ApplicationContext
You can use the command line to view the number of Context
adb shell dumpsys meminfo package_name


Ii. Question 1. What is the difference between getBaseContext and getApplicationContext? The Context holding the Activity is equivalent to holding the Context, and the AppliactionContex holding only has this
2. Where does Context in the view come from? Example: new TextView (Context );
Generally, the current Activity or Activity. getBaseContext () is input in an Activity, so View. getContext () is actually a reference of the current Activity.
In common scenarios, the Adapter usually passes Context through the constructor for the inflate view during getView. However, the most common parameter in getView is parentView, which is the ListView object. You can use parentView. getContext to obtain the Context object to reduce manual transmission.
3. Where a Context error occurs, Dialog. Builder must pass in Activity, but not Activity. getApplicationContext ()
4. Use Context to view methods for resource access, create views, and create four components
What is Context?

Reference: Android source code analysis-a comprehensive understanding of ContextAndroid Context and Its Usage

3. memory overflow. 1. Context causes memory overflow: Avoiding memory leaks and Avoiding memory leaks.
Android-what's the difference between the various methods to get a Context?

For details about the above articles, refer to the article. The following is a simple description: The most common form of memory is that Bitmap is not released, and the image is usually held by ImageView, so ImageView will not be released by GC, context is required to create an ImageView. The Context is Activity. Bitmap-> ImageView-> Contex (Activity) if the Activity cannot be released, resulting in insufficient memory and eventually OOM

2. For objects with a long life cycle, use ApplicationContext. The following document describes how to easily obtain the Application Context by using a custom Application globally.
Use a custom Application to pass in the Context object to avoid memory overflow caused by holding the Context object. Because ApplicationContext has only one instance globally, and multiple activities inherit from Context, multiple Context instances are involved.
Another method for sharing variables of Activity in Android: Application context
Let's talk about the use of Context in Android !!!

4. Information on Context memory overflow Android Learning Series (36)-Context for App debugging Memory leakage (part I)
Android Learning Series (37)-Context of App debugging Memory leakage (II)

4. Create your own ContextAndroid to get the Context instance of other packages and then do bad http://chroya.iteye.com/blog/761441



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.