Skip to content

Using Context

kgleong edited this page Jul 16, 2015 · 68 revisions

Overview

Context provides an interface to global information about an application environment. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

What is a Context

Why are Contexts important?

System Resources

Themes

Providing Contexts

Context subclasses

Contexts follow the wrapper/decorator pattern that allow subclasses to add on only the specific feature sets required.

context subclasses

  • ContextWrapper
  • ContextThemeWrapper
  • Application
  • Activity
  • Service

Considerations

Avoiding Memory Leaks

Be careful when storing references to a context instance.

Improper storing of a context

In the example below, if the context being stored is an Activity or Service and it's destroyed by the Android system, it won't be able to be garbage collected because the CustomManager class holds a static reference to it.

To avoid memory leaks, never hold a reference to a context beyond its lifespan.

This can also be a problem when background threads, pending handlers, or inner classes hold onto context objects as well.

pubic class CustomManager {
    private static CustomManager sInstance;

    public static CustomManager getInstance(Context context) {
        if (sInstance == null) {

            // This class will hold a reference to the context
            // until it's unloaded. The context could be an Activity or Service.
            sInstance = new CustomManager(context);
        }

        return sInstance;
    }

    private Context mContext;

    private CustomManager(Context context) {
        mContext = context;
    }
}

Proper storing of a context: use the application context

Store the application context in CustomManager.getInstance(). The application context is a singleton and is tied to the lifespan of application's process, making it safe to store a reference to it.

Use the application context when a context reference is needed beyond the lifespan of a component, or if it should be independent of the lifecycle of the context being passed in.

public static CustomManager getInstance(Context context) {
    if (sInstance == null) {

        // When storing a reference to a context, use the application context.
        // Never store the context itself, which could be a component.
        sInstance = new CustomManager(context.getApplicationContext());
    }

    return sInstance;
}

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally