Overview of the Android context principle and use

Source: Internet
Author: User



First, the context inheritance system and the context is how to create 1. Context Inheritance System
Just remember: Activity, Service and application are inherited from Contextwrapper, and Contextwrapper implements Context.

Each: Activity, Service, and application are all instances of a context.

2. When the Context was created and how it was created-view source code
Analysis of the creation process of the execution context environment (context) of the Android application form (Activity)

Android Kernel--context Object

The context in Android specifically explains----The context you don't know
The above 3 articles all analyze when the context was created from the source code perspective. But for the usual development, only need to know that activity and service are inherited from the context. Just creating a new activity or Service instance creates a new context instance.
Total Context = number of activity + number of Service + 1 ApplicationContext
Ability to view the number of context from the command line
adb shell Dumpsys meminfo package_name


Ii. questions about the context 1. Getbasecontext and Getapplicationcontext difference? The context in which the activity is held is equivalent to holding the context. and holding Appliactioncontex global only this one
2. Where does the context in the view come from? For example: New TextView (Context);
The current activity or activity.getbasecontext () is usually passed into an activity. So through View.getcontext () is actually a reference to the current activity.
Common scenarios, adapter typically pass the context through the constructor, for GetView inflate views. But GetView most of the parameters are Parentview this is the ListView object itself. The ability to get a context object through Parentview.getcontext reduces manual delivery.
3. Where the Context will go wrong dialog.builder must pass in the activity and cannot pass in Activity.getapplicationcontext ()
4. Context role, view methods to access resources, create views, create four components
What is the context?

Reference information: Android source code Analysis-A comprehensive understanding of the context in contextandroid and how to use it

Three memory overflows. Because the reference context causes 1. Context causes memory overflow: avoiding leaks, avoiding memories leaks
Android-what ' s The difference between the various methods to get a Context?

The above article explains the very specific can view the article. Here is a brief description of the narrative: the most common form of memory is bitmap is not released, and the picture is usually imageview holding leads to ImageView will not be released by the GC, the creation of ImageView certainly need context, this context is activity. Contex (activity), ImageView, Bitmap, assumes that activity is always not released, causing insufficient memory to finally Oom

2. For objects with a very long life cycle, using ApplicationContext, the following document describes the method by which you define application to be able to get application context very easily in a project
Using your own definition of application, you need to pass in the context object. Avoid memory overflows caused by holding the context. Since there is only one instance of the ApplicationContext global, and multiple activity itself inherits from the context, it is multiple context instances.
There is another way to share variables in Android: Application context
Talk about the use of the context in Android!!!

4. Context memory overflow related information Android Learning Series (--APP) Debug memory leak context (Part One)
Android Learning Series (PNS)--app Debug Memory leak context (bottom)

Iv. Create your own contextandroid get the context instance of the other package and then do something bad http://chroya.iteye.com/blog/761441



Overview of the Android context principle and use

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.