Still confusion about the context

I am new to android ...

The maximum time that I came across a class contextthat sometimes makes me confused. In some places, use context, somewhere it is called getApplicationContext(), getContext(), getBaseContext().

I tried to make me understand about this from this site, http://www.developer.android.com/ , but it was hard to understand.

+5
source share
1 answer

In Android, context is used for many operations, but mainly for loading and accessing resources. That is why all widgets receive the Context parameter in their constructor. In a typical Android application, you usually have two kinds of context, activity, and application. Usually this is the first one that the developer passes to classes and methods that need context:

You can get the context by calling getApplicationContext (), getContext (), getBaseContext () or this (when in an activity class).

Typical use of context:

  • Creating new objects: creating new views, adapters, listeners:

    TextView tv = new TextView (getContext ()); Adapter ListAdapter = new SimpleCursorAdapter (getApplicationContext (), ...);

  • Access to standard shared resources: services such as LAYOUT_INFLATER_SERVICE, SharedPreferences:

    context.getSystemService(LAYOUT_INFLATER_SERVICE)
    getApplicationContext(). getSharedPreferences (, );

  • : , ,

    getApplicationContext(). getContentResolver(). query (uri,...);

, maintaning

, , , :

  • - ( , )
  • , , . WeakReference , ViewRoot W,
  • .
+2

All Articles