Some summary about Android context

Source: Internet
Author: User

1. What is the context first?

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

2. Why do I need context?

Imagine this statement:

Button MyButton =newbutton (this);

This is the context, which says it points to the parent object, where does that point? Why do you need to do this? The reason is that button in order to complete his mission (in response to a variety of actions) will have to let the Android system know which activity they belong to, this information is necessary, because only then the Android system will manage it, such as the response to the onclick () event, Otherwise, the system even button belongs to which activity is not known, how to respond? If the context of the button is the same as the context of the activity, then they are visible, such as the button above is visible to the activity, and their context points to another invisible object. That is, they are controlled by the invisible object, which I understand as the system.

3.Application context and activity context.

This is two different kinds of context and the most common. The life cycle of the first middle context is related to the life cycle of the application, the context is destroyed with the destruction of the application, and the second context 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 for which context to look at the application scenario, the individual feels better with the context of the activity, but sometimes must use the application context.

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

First, the establishment of Sqliteopenhelper sub-class

public class Mysqlitehelper extends Sqliteopenhelper {

..........

Public Dbopenhelper (Context context, String name, Cursorfactory factory,

int version) {

Super (context, name, Factory, version);

TODO auto-generated Constructor stub

}

.......

}

Second, we use another class to encapsulate this class, perfect data query, insert, delete, update and other operations, the specific method does not table.

public class DBManager {

Define the class object above;

Private Mysqlitehelper DBHelper;

Note here to define a context, you want to directly get is not available, you need to call this class of the object itself to pass a context, the context will be the context of the resulting mysqlitehelper to the class to go.

Private context context;

Construction method

Public DBManager (Context context) {

This.context = context;

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

db = Dbhelper.getwritabledatabase ();

}

}

Third, finally, we call in an activity

public class MyActivity extends Activity {

Private DBManager DB;

public void OnCreate (Bundle savedinstancestate) {

........

Dbmgr = new DataManager (Getapplicationcontext ()); First Kind

Dbmgr = new DataManager (this); The second Kind

Both methods are available, the first is the context of the application, and the second is the context of the activity

.........

}

}

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.