Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use ContextCompat class

Tags:

android

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?

like image 494
harshita Avatar asked Mar 29 '17 07:03

harshita


3 Answers

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

like image 114
Andrey Danilov Avatar answered Nov 20 '22 13:11

Andrey Danilov


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.

like image 28
mpals Avatar answered Nov 20 '22 14:11

mpals


basically according to the official developer site it is a Helper for accessing features in Context introduced after API level 4 in a backwards compatible fashion.

You can look into this link for more details. https://developer.android.com/reference/android/support/v4/content/ContextCompat.html

Basically getBackgroundResource or getColor method are deprecated and using ContextCompact is an alternative for that. I hope this helps.

like image 2
rogue_shadow Avatar answered Nov 20 '22 12:11

rogue_shadow