Q:

When to use ContextCompat class

I want to know when to use ContextCompact class in an application. Basically what is it used for and when to use it? I have read developers site, it says ContextCompact is a "helper for accessing features in Context". But what does this line mean?

A:

ContextCompat is a class for replacing some work with base context.
For example if you used before something like
getContext().getColor(R.color.black);

Now its deprecated since android 6.0 (API 22+) so you should use:
getContext().getColor(R.color.black,theme);

or use ContextCompat which fill theme automatically depends on your Context's theme:
ContextCompat.getColor(getContext(),R.color.black)

Same thing with getDrawable
Also ContextCompat contains other methods for functional of API 22+ such as checking permissions or adding multiple activity to stack

A:

ContextCompat class is used when you would like to retrieve resources, such as drawable or color without bother about theme. It provide uniform interface to access resources and provides backward compatibility.
Common use case could be get color or drawable etc e.g..

ContextCompat.getDrawable(context, R.drawable.someimage));
  ContextCompat.getDrawable(context, R.color.blue));

Lets see the source code of getColor()
/*
 * Returns a color associated with a particular resource ID
 * <p>
 * Starting in {@link android.os.Build.VERSION_CODES#M}, the returned
 * color will be styled for the specified Context's theme.
 *
 * @param id The desired resource identifier, as generated by the aapt
 *           tool. This integer encodes the package, type, and resource
 *           entry. The value 0 is an invalid identifier.
 * @return A single color value in the form 0xAARRGGBB.
 * @throws android.content.res.Resources.NotFoundException if the given ID
 *         does not exist.
 */
@ColorInt
public static final int getColor(Context context, @ColorRes int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompatApi23.getColor(context, id);
    } else {
        return context.getResources().getColor(id);
    }
}

this method takes care the API level resolution and resolve states or theme automatically. Above 23, the color states are accessible, which is internally resolved for you, instead you should check it for each resource.

