Android Context Introduction

Source: Internet
Author: User
Tags resource

First, what is context?

The document is vague, say is resource what, anyway is not understand, can actually think it is a pointer to the parent object, subject to the control of the parent object.

Two. Why do I need context?

Consider this statement:

Button MyButton =newbutton (this);

This is the context, and it points to the parent object. Why do you need to do this? The reason is that button is necessary to let the Android system know which activity it belongs to in order to fulfill its mission (in response to various actions), because only then will the Android system manage it, such as responding to the OnClick () event, Otherwise the system even button belongs to which activity does not know, how to respond to it? If the context of the button is the same as the context of the activity, then they are visible, for example, the button above is as visible as the activity, and their context points to another invisible object. That is, they are controlled by that invisible object, which I understand as the system.

Three. Application context and Activity context

This is the two different context, and the most common two kinds. The life cycle of the first context is related to the life cycle of the application, and the context is destroyed with the destruction of the application, and the second is related to the life cycle of the activity, But for a application, the activity can be destroyed several times, then the context of the activity will be destroyed many times. As to which context to use, see the application scene, personal feeling with the context of activity better, but also sometimes must use the context of application.

For example: When SQLite is created, it needs context, so how do we pass it on?

1. Establishment of Sqliteopenhelper subclass

     {

..........

 (Context, String name, Cursorfactory Factory,

 version) {

(context, name, Factory, version);



}

.......

}

2. We use another class to encapsulate this class, improve the data query, insert, delete, update and other operations, the specific method does not table

  Dbmanager {



 mysqlitehelper dbhelper;





 Context context;





  (Context context) {

. Context = context;

DBHelper =  Dbopenhelper (context,db_name,,version);

db = Dbhelper.getwritabledatabase ();

}

}

3. Finally, we make a call in an activity

     {

 Dbmanager db;

   (Bundle savedinstancestate) {

........

     

       Dbmgr =  DataManager ();  



.........

}

}

The context can do a lot of things in Android, but the main function is to load and access resources. There are two kinds of context in Android, one is the application, the other is the context of activity, and we usually pass the activity context among various classes and methods.
such as the oncreate of an activity:

  (Bundle State) {
        . onCreate (state);

        TextView label =  TextView (); 
        Label.settext ();

        Setcontentview (label);

Passing the activity context to view means that the view has a reference to the activity, which in turn refers to the resources the activity occupies: view hierachy, Resource, and so on. This will reveal a lot of memory if there is a memory leak in the context. The leak here means that the GC has no way to reclaim the activity's memory. The activity of leaking an entire is very easy.

When the screen rotates, the system destroys the current activity, saves state information, and creates a new one.

For example, we write an application, it needs to load a large picture, we do not want to rotate the screen every time we destroy the picture, reload. The simple idea of implementing this requirement is to define a static drawable so that the activity class creates and destroys it and is always saved in memory.
Implement similar:

     {
          drawable sbackground;
          (Bundle State) {
                . onCreate (state);

                TextView label =  TextView ();
                Label.settext ();

                 (Sbackground = =) {
                        Sbackground = getdrawable (R.drawable.large_bitmap);
                }
        Label.setbackgrounddrawable (sbackground);

        Setcontentview (label);
        }

The program looks simple, but it's a big problem. There is a leak when the screen rotates (that is, the GC cannot destroy the activity).

We have just said that the system destroys the current activity when the screen rotates. However, when drawable is associated with the view, Drawable saves the reference of the view, that is, Sbackground holds the reference to the label, and the label holds the reference to the activity. Since drawable cannot be destroyed, neither the reference nor the indirect reference can be destroyed, so that the system has no way to destroy the current activity, resulting in a memory leak. The GC is powerless for this type of memory leak.

The way to avoid this memory leak is to avoid the lifecycle of any object in the activity and to avoid the activity being destroyed as a result of the object's reference to the activity. We can use the application context. The application context accompanies the life of application, irrespective of the lifecycle of the activity. The application context can be obtained by Context.getapplicationcontext or Activity.getapplication methods.

To avoid a context-related memory leak, keep the following points in mind:

Do not allow long-lived objects to refer to the activity context, that is, to ensure that the object referencing the activity is the same as the activity itself lifecycle

For objects with long life cycles, you can use the application context

Avoid non-static internal classes, use static classes as much as possible, avoid lifecycle problems, and note the life cycle changes caused by internal classes to external object references

The context literal means the contextual, in the Android.content.Context of the framework package, in fact the class is long, similar to the handle handle in Win32. Many methods require the context to identify the caller's instance: for example, the first parameter of toast is the context, which is usually used directly in the activity, representing the instance of the caller as an activity, When we go to a button's onclick (view view) method, we use this to make an error. So we might use activityname.this to solve the problem, mainly because the classes that implement the context mainly have several Android-specific models, activity, and service.

As shown in the following illustration:

The context provides an interface for global information about the application environment. It is an abstract class, and its execution is provided by the Android system. It allows access to resources and types that are characterized by application. Start an application-level operation at the same time, such as starting activity,broadcasting and receiving intents.

Four, HANDLE HANDLE handle is what meaning, he is a pointer?

A handle refers to a unique integer value that is used, refers to a four-byte long value that is used to flag different objects in an application and to various instances of the same object, such as a window, button, icon, scroll bar, output device, control, or file, and the application can access the corresponding object's information through the handle. However, the handle is not a pointer, and the program cannot use its handle to directly read the information in the file.

Thanksgiving:

Android Context Profile: http://blog.csdn.net/zhangqijie001/article/details/5891682

Some views on Android context: http://blog.sina.com.cn/s/blog_80bfa06701014c9g.html

What is context in Android? : http://blog.csdn.net/race604/article/details/9331807

How to use the context of Android: http://www.cnblogs.com/thinksasa/archive/2012/12/01/2796964.html

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.