When to use ContextCompat class

后端 未结 3 2012
梦谈多话
梦谈多话 2021-02-07 02:00

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 Cont

相关标签:
3条回答
  • 2021-02-07 02:40

    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.

    0 讨论(0)
  • 2021-02-07 03:01

    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.

    0 讨论(0)
  • 2021-02-07 03:04

    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

    0 讨论(0)
提交回复
热议问题