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 both a context instance.

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

Android Kernel--context Object

Context in Android----The context you don't know
The above 3 articles are from the source point of view to analyze when the context was created, but for peacetime development, only need to know that activity and service are inherited from the context, as long as the creation of new activity or service instances, is to create a new context instance.
Total Context = number of activity + number of Service + 1 ApplicationContext
The number of context can be viewed from the command line
adb shell Dumpsys meminfo package_name


Ii. questions about the context 1. What is the difference between Getbasecontext and getapplicationcontext? The context in which the activity is held is equivalent to holding the context, while holding the 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 in an activity, so 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, can be obtained by parentview.getcontext the context object to reduce 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 access resources, create views, create four components
What is the context?

Reference: Android source Code Analysis-A comprehensive understanding of the context in contextandroid and its usage

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 article explained in detail can be viewed in the article, the following is a simple description: The most common form of memory is bitmap is not released, and the picture is usually ImageView hold cause ImageView will not be released by GC, create ImageView definitely need the context, This context is activity. Bitmap, ImageView, Contex (activity) If the activity is always unable to be released, resulting in insufficient memory eventually oom

2. For objects with a long life cycle, using ApplicationContext, the following document describes how custom application can easily get application context in a project
With custom application, a context object is required to pass in, avoiding memory overflow caused by holding the context. Because there is only one instance of the ApplicationContext global, and multiple activity itself inherits from the context, it is multiple context instances.
Another way for activity sharing 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.